Author |
Topic |
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2012-08-17 : 12:38:21
|
Hi I'm trying to update a table of 25,000 rows with a sequential unique number beginning with "P" for producte.g.P000001P000002P000003etcI can start from 1 and just keep going up.I think I need to declare an Int variable but I am not sure howto do that.If anyone can help it would really help me outthanks in advance |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-08-17 : 12:49:43
|
something like this:update t1set YourNewColumn = 'P' + right('000000' + convert(varchar(10),rnum),6)from YourTable t1join(select row_number() over (order by (select 1)) as rnum,* from YourTable) dton t1.IdColumn = dt.IdColumn Too old to Rock'n'Roll too young to die. |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2012-08-18 : 00:39:39
|
Thanks - both replys were helpful.I've done it!! |
 |
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2012-08-25 : 21:42:47
|
You can do it without having to do 2 table scans. Most people don't know that you can update through a CTE like this...WITH cte AS( SELECT CalculatedProductNumber = 'P' + RIGHT('000000' + CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS VARCHAR(6)),6), Product FROM #SomeTable) UPDATE cte SET Product = CalculatedProductNumber --Jeff Moden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-09-03 : 09:28:27
|
quote: Originally posted by Jeff Moden You can do it without having to do 2 table scans. Most people don't know that you can update through a CTE like this...WITH cte AS( SELECT CalculatedProductNumber = 'P' + RIGHT('000000' + CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS VARCHAR(6)),6), Product FROM #SomeTable) UPDATE cte SET Product = CalculatedProductNumber --Jeff Moden
Yes. It is also possible to delete through a CTE MadhivananFailing to plan is Planning to fail |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-09-03 : 09:33:20
|
quote: Originally posted by icw Hi I'm trying to update a table of 25,000 rows with a sequential unique number beginning with "P" for producte.g.P000001P000002P000003etcI can start from 1 and just keep going up.I think I need to declare an Int variable but I am not sure howto do that.If anyone can help it would really help me outthanks in advance
Also read this http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=57069MadhivananFailing to plan is Planning to fail |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-09-03 : 10:13:08
|
quote: Originally posted by madhivanan
quote: Originally posted by Jeff Moden You can do it without having to do 2 table scans. Most people don't know that you can update through a CTE like this...WITH cte AS( SELECT CalculatedProductNumber = 'P' + RIGHT('000000' + CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS VARCHAR(6)),6), Product FROM #SomeTable) UPDATE cte SET Product = CalculatedProductNumber --Jeff Moden
Yes. It is also possible to delete through a CTE MadhivananFailing to plan is Planning to fail
And merge. Especially merge....Practically the entirety of uses I have for CTE are due to merges.Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
 |
|
|