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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 auto generated customised primary key

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-09-08 : 09:03:54
rk writes "I have to write a stored procedure which wil create automated an alphanumeric primary key in the background.


---this is for K type customer
say:- if the previous key 'k002' then it has to generate 'K003' but again after 'k009' it has to generate 'k10'

-- this is for R type customer.
it start with rseries and it has to increse as above start with r."

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-09-08 : 09:27:00
Something along these lines:

DECLARE @MAXKey INT
DECLARE @NewKey VARCHAR(10)

SET @MaxKey = (SELECT MAX(CAST(RIGHT(PrimaryKey, 9) as INT)) FROM TableA)
SET @NewKey = 'K' + RIGHT('000000000' + LTRIM(RTRIM(STR(@MaxKey + 1, 9))))



Duane.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-08 : 09:31:53
this would help too:
http://www.sqlteam.com/item.asp?ItemID=1417

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -