Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have created a table with an int primary key that is set to automatically increase.

But, customer neeeds some readable key for each row, for instance PRD-12-00001 where 12 is the year and 00001 a correlative number.

For this purpose I have created an ID table with two fields, Key and Number.

I need an UDF that retrieves the number from this table and increments this number.

Is there any sample over there?

Thanks in advance
Antxon
Posted

1 solution

Try something like:
SQL
CREATE TABLE [dbo].[empWithTrigger](
        [sal] [decimal](10, 2) NULL,
        [code] [varchar](50) NULL,
        [tag] [varchar](50) NULL,
        [id] [int] IDENTITY(1,1) NOT NULL,
        [alphanumeric] [varchar](50)
)
GO

Create TRIGGER alphanumericColumn
   ON  empWithTrigger
   AFTER  INSERT
AS
BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;

UPDATE a
SET    alphanumeric = 'ABC' + LEFT('00' + CAST(inserted.id AS VARCHAR(10)), 3)
FROM   empWithTrigger a
       INNER JOIN inserted
         ON a.id = inserted.id

END
GO


Discussion related to it here: how to auto increment alphanumeric primary key..[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900