elams (me*********@dbforums.com) writes:
How to generate a seq number in TSQL. My need is for every record
insert ,I need to insert a seq number paded with letter 'I'.
The normal way to achieve something like this is to use one of the
following to methods:
o Declare a column to have the IDENTITY property and pick up the
value with the scope_identity() function.
o Retrieve the current max value and then add one:
SELECT @nextid = coalesce(MAX(id), 0) + FROM tbl (UPDLOCK)
This must be done in a transaction to prevent two processes getting
the same ID.
Now, if I understand you correctly, you want the value to be I1, I2, I3,
etc. In this case, none of the methods above work directly, because
the I confuse matters.
My first attempt would be to see if I could somehow get this "I" out of
the design, because that would simplify things a lot.
If business reasons require presence of the letter, I would look into
to make the column with I1, I2 etc a computed column, where the forumla
would be 'I' + str(ltrim(id)), where id is the name of a numeric column
as above. Probably the column should be indexed, so that you can use it
for lookup from user input. Reference to the table from other tables
would be on the numeric column though.
--
Erland Sommarskog, SQL Server MVP,
so****@algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp