Solved

autonumber with a variable prefix

Posted on 2006-06-22
3
535 Views
Last Modified: 2009-12-16
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
Comment
Question by:rutgermons
3 Comments
 
LVL 6

Expert Comment

by:davbouchard
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

by:rutgermons
ID: 16962363
could u assist with an example pls?

r
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 500 total points
ID: 16974848
SOMETHING LIKE THS




CREATE TRIGGER [TRIGGER NAME] ON [dbo].[test]
instead of  INSERT
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now