SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 sequential incremental number
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

icw
Constraint Violating Yak Guru

378 Posts

Posted - 08/17/2012 :  12:38:21  Show Profile  Reply with Quote
Hi I'm trying to update a table of 25,000 rows with a sequential unique number beginning with "P" for product
e.g.
P000001
P000002
P000003
etc

I 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 out
thanks in advance

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 08/17/2012 :  12:43:32  Show Profile  Reply with Quote
see

http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 08/17/2012 :  12:49:43  Show Profile  Visit webfred's Homepage  Reply with Quote
something like this:

update t1
set YourNewColumn = 'P' + right('000000' + convert(varchar(10),rnum),6)
from YourTable t1
join
(select row_number() over (order by (select 1)) as rnum,* from YourTable) dt
on t1.IdColumn = dt.IdColumn


Too old to Rock'n'Roll too young to die.
Go to Top of Page

icw
Constraint Violating Yak Guru

378 Posts

Posted - 08/18/2012 :  00:39:39  Show Profile  Reply with Quote
Thanks - both replys were helpful.
I've done it!!
Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
643 Posts

Posted - 08/25/2012 :  21:42:47  Show Profile  Reply with Quote
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

Edited by - Jeff Moden on 08/25/2012 21:45:50
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 09/03/2012 :  09:28:27  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 09/03/2012 :  09:33:20  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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 product
e.g.
P000001
P000002
P000003
etc

I 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 out
thanks in advance


Also read this http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=57069

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3430 Posts

Posted - 09/03/2012 :  10:13:08  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
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

Madhivanan

Failing to plan is Planning to fail



And merge. Especially merge....

Practically the entirety of uses I have for CTE are due to merges.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000