Solved

autonumber with a variable prefix

Posted on 2006-06-22
3
557 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
[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
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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