| Author |
Topic |
|
Tim_Field
Yak Posting Veteran
87 Posts |
Posted - 2005-10-11 : 08:23:28
|
| /* I'd like to know how to do this for the next 6 rows... Basically this is a cut down version of an insert statement. How do I get the year to -1 each time.Obviously my code doesn't work!!!*/declare @todaysyear intselect @todaysyear = 2005declare @yeartable table (myyear int)insert into @yeartable(myyear)values(@todaysyear = @todaysyear - 1) |
|
|
Tim_Field
Yak Posting Veteran
87 Posts |
Posted - 2005-10-11 : 08:26:26
|
| Oh yeah, forgot to mention, I can obviously use a "for" loop but I'd rather not go this route as I'll be selecting from another table in my final insert. |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2005-10-11 : 08:29:25
|
| declare @todaysyear int, @x as intselect @todaysyear = 2005, @x=0declare @yeartable table (myyear int)while @x < 6BEGIN insert into @yeartable (myyear) values (@todaysyear-@x) set @x = @x + 1 ENDselect * from @yeartable"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
Tim_Field
Yak Posting Veteran
87 Posts |
Posted - 2005-10-11 : 08:33:36
|
| yep that's what I meant I don't want to use a for \ while loop etc.Maybe I'm thinking this through too much!I suspect that creating an identity column that starts at 2005 in a temporary table may work (somehow!). |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2005-10-11 : 08:36:18
|
| declare @todaysyear int, @y as intselect @todaysyear = 2005, @y=0declare @yeartable table (myyear int)declare @xtable table (x int)while @y < 10begin insert into @xtable values (@y) set @y=@y+1endinsert into @yeartable (myyear) select top 3 @todaysyear - x from @xtableselect * from @yeartable"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
Tim_Field
Yak Posting Veteran
87 Posts |
Posted - 2005-10-11 : 08:55:44
|
| Thanks jhocutt... I have a few ideas now. Appreciate the help.hmmm annoyingly I can't do this!.declare @yearstable table (myyear_id int identity(@acad_year_to_use, 1), myyear int, myyear_minus_1 int) |
 |
|
|
AjarnMark
SQL Slashing Gunting Master
3246 Posts |
Posted - 2005-10-11 : 13:11:27
|
| Tim, check out the technique that Graz writes about in his article Creating a Sequential Record Number Field. It sounds like just what you're trying to accomplish. Using an INSERT ... SELECT, and a - instead of + and you should be good to go.---------------------------EmeraldCityDomains.com |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2005-10-11 : 13:51:04
|
| Very cool AjarnMark, Thanks for the tip."God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
rfrancisco
Yak Posting Veteran
95 Posts |
Posted - 2005-10-11 : 16:01:25
|
| An alternative is to use a table-valued user-defined function that will return a table containing the years you want. Here's a link to such a function that you can use:http://www.sql-server-helper.com/functions/integer-table.aspxIn that link, there are 2 versions of the same function. You can use the one where you can specify the starting value and the ending value. Then you can use this function just like an ordinary table. |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-10-12 : 08:08:11
|
| This is kinda dumb, but it meets your requirement for not using a loop:declare @yeartable table (myyear int)declare @lastyear intselect @lastyear = 2006insert into @yeartable select 2005 from (select top 6 1 a from sysobjects) aupdate @yeartableset @lastyear = myyear = @lastyear - 1select * from @yeartableJay White |
 |
|
|
|