Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
wooldawg
Starting Member
12 Posts |
Posted - 2008-04-24 : 09:43:39
|
| Hello all, I'm not even sure what you call this kind of update statement so I wasn't able to search the forums before making new post. Say I have a table looks like this:Name: Year:Bob 2007Sue 2007Jane 2007Ralph 2008Sue 2008Jane 2008What I need to do is insert all the names from the most current year back into the table with the next year so that after the update the table would look like:Name: Year:Bob 2007Sue 2007Jane 2007Ralph 2008Sue 2008Jane 2008Ralph 2009Sue 2009Jane 2009This seems simple enough but has me completely stumped. Thanks for your help! |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-24 : 09:47:28
|
| [code]declare @t table (name_col varchar(10), year_col int)insert into @tselect 'bob',2007union all select 'sue',2007insert into @tselect name_col,max(year_col)+1from @tgroup by name_colselect * from @t[/code]Em |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-24 : 09:52:35
|
INSERT Table1 (Name, Year)SELECT Name, DATEPART(YEAR, GETDATE()) FROM Table1 WHERE Year = DATEPART(YEAR, GETDATE()) - 1 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
wooldawg
Starting Member
12 Posts |
Posted - 2008-04-24 : 10:15:17
|
| Thanks Peso, this is exactly what I was looking for. |
 |
|
|
|
|
|