I suggest creating a table with two columns and one row to track the "serial number".
Create Table SerialNo (RecID int, SerNo int)
/* initialize the table */
Insert SerialNo values(1,0)
Then create a stored procedure to insert the new record in the table. Call the stored procedure and pass variablkes to be inserted into the table.
Create proc spInsertRec @var1 varchar(10), @var2 int, ... As
Declare @key char(8)
declare @serno int
/* Update the serial number table and return the new value in @serno variable */
Update SerialNo Set @serno=serno=serno+1
Where RecID=1
/* Create the key */
Select @key = 'X' +
right(convert(char(4), year(getdate())),2) +
replace(str(@serno,5),' ','0')
/* Insert record in the table. Assume all values are passed to SP except the key. */
Insert tbl values (@key, @var1, @var2, ....)
Hope this makes sense to you. Please ask questions if something isn't clear or doesn't work. Terry Broadbent
"The greatest obstacle to discovery is not ignorance -- it is the illusion of knowledge." - Daniel J Boorstin