| Author |
Topic |
|
shabad
Starting Member
9 Posts |
Posted - 2009-01-19 : 03:26:38
|
| hi ,I am trying to update a table with incremental value, and i need some help.table columns are:IDENTITY, ITEM, DESC, ID, SUBITEMFor occurance of each subitem i need the values for the Item from 1.The table is already populated with ID as 1 , and it has many ITEM's which inuten has many SUBITEM'sI wrote the update statement but get the max(id) dint work , i think my code is not correct.select @max_id = max(id) from Table1 where item = abcde UPDATE TABLE1 SET id = @max_id+1 from Table1 where table1.item = abcde This code updates the total occurances of each item for all the rowsInstead i need the output like the following :ROW IETM ID SUBITEM1 A 1 A12 A 2 A23 A 3 A34 A 4 A45 B 1 B16 B 2 B27 C 1 C1In the current table the ID is all set to 1can some one help me plzThanks in AdvanceShabad |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-19 : 03:37:47
|
| [code]UPDATE tSET t.ID=t.SeqFROM(SELECT ROW_NUMBER() OVER (PARTITION BY Item Order BY SubItem) AS SeqFROM Table)t[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-19 : 03:42:40
|
Will not work. The exposed name ID is not available from the derived table.Use thisUPDATE tSET t.ID=t.SeqFROM ( SELECT ID, ROW_NUMBER() OVER (PARTITION BY Item Order BY SubItem) AS Seq FROM Table1 ) AS t E 12°55'05.63"N 56°04'39.26" |
 |
|
|
shabad
Starting Member
9 Posts |
Posted - 2009-01-19 : 13:37:35
|
| Thanks for the help. but when i try to run it says row_number() is not a recognized function name. i am using sql server 2005 object explorer. the insertion and rest logic is written in a stored procedure.plz let me know how to go abt in this issue, or ant other work round.Thanks |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-19 : 14:55:58
|
TO use the new windowed functions available in SQL Server 2005, you must also set the compatibility level to 90 or higher. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
shabad
Starting Member
9 Posts |
Posted - 2009-01-19 : 16:48:35
|
| I am new to SQL Server can u plz eloborate on this so that i can get this one done. Your help and time is really appricatedThanks |
 |
|
|
shabad
Starting Member
9 Posts |
Posted - 2009-01-19 : 17:02:42
|
| I tried this one EXEC sp_dbcmptlevel AdventureWorks, 90;GObut the msg says it can be 60/65/70/80.The version of Sql server i am using is 2005 Microsoft SQL Server Management Studio 9.00.3042.00Microsoft Analysis Services Client Tools 2005.090.3042.00Microsoft Data Access Components (MDAC) 2000.086.3959.00 (srv03_sp2_rtm.070216-1710)Microsoft MSXML 2.6 3.0 4.0 5.0 6.0 Is this to do anything with the admin rights ???Or is there any other alternative method to do this like adding a temp table or something. I did try that but seems not working |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
shabad
Starting Member
9 Posts |
Posted - 2009-01-19 : 18:16:20
|
| Thats right , i used the @@version and i am using sql server 2000 with 2005 tools may be thats the reason row_number() is not working.But this still leaves the question where i started now , i need to update the table , need some helpThanks |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2009-01-19 : 21:40:19
|
quote: Originally posted by shabad Thats right , i used the @@version and i am using sql server 2000 with 2005 tools may be thats the reason row_number() is not working.But this still leaves the question where i started now , i need to update the table , need some helpThanks
You can simulate the row_number() function by modifying the code peso posted with something like this:UPDATE tSET t.ID=t.SeqFROM ( SELECT ID,(select count(*) from Table1 t2 where t2.subitem <= t1.subitem and t2.item = t1.item ) AS Seq FROM Table1 t1 ) AS t |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2009-01-19 : 21:42:44
|
quote: Originally posted by Peso TO use the new windowed functions available in SQL Server 2005, you must also set the compatibility level to 90 or higher. E 12°55'05.63"N 56°04'39.26"
You don't need to. If the database is hosted on a SQL 2005 server, you can use the windowing functions even if the database compatibility level 80. |
 |
|
|
shabad
Starting Member
9 Posts |
Posted - 2009-01-20 : 18:22:25
|
| Thanks for the help but still i am having problemsi tried the sequence creation but it gives me the following errorUPDATE tSET t.id=t.SeqFROM ( SELECT id,(select count(*) from #Materials2 t2 where t2.t_sitm <= t1.t_sitm and t2.prev_item = t1.prev_item ) AS SeqFROM #Materials2 t1) AS tDerived table 't' is not updatable because a column of the derived table is derived or constant.Am i making any mistake in the query ? |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2009-01-20 : 20:57:25
|
| SQL 2000 does not allow updating derived tables. SQL 2005 does.You can update the table directly as:update #Materials2 set id = ( select count(*) from #Materials2 t2 where t2.t_sitm <= #Materials2.t_sitmand t2.prev_item = #Materials2.prev_item )) |
 |
|
|
|