Solved

autonumber with a variable prefix

Posted on 2006-06-22
3
529 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

706 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

18 Experts available now in Live!

Get 1:1 Help Now