Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# autonumber with a variable prefix

Posted on 2006-06-22
Medium Priority
586 Views
folks

I have an autonumber eg BTU 1001

when I hit insert it has to be BTU1002

though the prefix BTU can be different

so I might have

CTU 233

the next would be CTU234

how can I build a trigger

so when I insert a new record i.e with the prefix

BTU and then save it it will take the last number and add one eg

BTU1003  (BTU1002+1)

when I then put in

CTU and save then CTU235 will crop up

all help will do

rutger

0
Question by:rutgermons
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 6

Expert Comment

ID: 16962343
Create a table that has 2 columns  Prefix (char(xx)) and NextNumber (int)

Inside the trigger

1. look up the NextNumber for that prefix
2. Grab it
3. Update that record with the next number

If the Prefix isn't found, insert it and start at 1 or whatever number you want.

0

Author Comment

ID: 16962363
could u assist with an example pls?

r
0

LVL 28

Accepted Solution

imran_fast earned 2000 total points
ID: 16974848
SOMETHING LIKE THS

CREATE TRIGGER [TRIGGER NAME] ON [dbo].[test]
AS
BEGIN
DECLARE @COLUMN VARCHAR(10)

SELECT @COLUMN = COL FROM INSERTED
INSERT INTO  TEST (COL)
SELECT @COLUMN +  CAST (ISNULL(MAX(CAST(REPLACE(COL,@COLUMN ,'') AS INT) +1),0) AS VARCHAR(10))  FROM TEST WHERE COL LIKE '%'+@COLUMN+'%'
END
GO
INSERT INTO TEST VALUES('CTU')
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
###### Suggested Courses
Course of the Month6 days, 16 hours left to enroll