By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Using a trigger to generate an ID value

P: n/a
The table in the database has a field called LabID. That field is an
integer and consists of the year plus a counter. For example, the
first record of 2006 would be "20060001," the second record of 2006
would be "20060002" and so on. I'm trying to create an Insert trigger
that can generate the ID value when a new record is inserted, but I'm
not quite sure how to implement that trigger. Can anyone help?

Sep 15 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
im*******************@yahoo.com (im*******************@yahoo.com) writes:
The table in the database has a field called LabID. That field is an
integer and consists of the year plus a counter. For example, the
first record of 2006 would be "20060001," the second record of 2006
would be "20060002" and so on. I'm trying to create an Insert trigger
that can generate the ID value when a new record is inserted, but I'm
not quite sure how to implement that trigger. Can anyone help?
The script below demonstrates. Note that it presumes that the table
already has an identifyable key, else there is no possibility to handle
multi-row inserts.

The solution presumes SQL 2005.

CREATE TABLE nisse (a int NOT NULL PRIMARY KEY,
LabID int NULL)
CREATE INDEX LabID_ix ON nisse(LabID)
go
CREATE TRIGGER nisse_tri ON nisse FOR INSERT AS
DECLARE @curmax int,
@curyear int,
@rowc int

SELECT @rowc = @@rowcount
IF @rowc = 0
RETURN

SELECT @curyear = year(getdate()) * 10000

SELECT @curmax = coalesce(MAX(LabID) - @curyear, 0)
FROM nisse (HOLDLOCK)
WHERE LabID BETWEEN @curyear + 1 AND @curyear + 9999

IF @curmax + @rowc 10000
BEGIN
ROLLBACK TRANSACTION
RAISERROR ('%d rows have already been inserted this year, so you cannot
insert %d rows now. Please wait until next year',
16, 1, @curmax, @rowc)
RETURN
END

UPDATE nisse
SET LabID = @curyear + @curmax + i.newlabid
FROM nisse n
JOIN (SELECT a, newlabid = row_number() over (ORDER BY a)
FROM inserted) AS i ON n.a = i.a
go
INSERT nisse (a)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (a)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 16 '06 #2

P: n/a
Thank you for the response. Please bear in mind that the LabID is the
identifiable key; it is the PK for the table. Also, I don't understand
why LabID = @curyear + @curmax + i.newlabid instead of @curyear +
(@curmax + 1). Finally, could you please explain to me why the UNION
statements are necessary?

Again, thank you very much.
Erland Sommarskog wrote:
im*******************@yahoo.com (im*******************@yahoo.com) writes:
The table in the database has a field called LabID. That field is an
integer and consists of the year plus a counter. For example, the
first record of 2006 would be "20060001," the second record of 2006
would be "20060002" and so on. I'm trying to create an Insert trigger
that can generate the ID value when a new record is inserted, but I'm
not quite sure how to implement that trigger. Can anyone help?

The script below demonstrates. Note that it presumes that the table
already has an identifyable key, else there is no possibility to handle
multi-row inserts.

The solution presumes SQL 2005.

CREATE TABLE nisse (a int NOT NULL PRIMARY KEY,
LabID int NULL)
CREATE INDEX LabID_ix ON nisse(LabID)
go
CREATE TRIGGER nisse_tri ON nisse FOR INSERT AS
DECLARE @curmax int,
@curyear int,
@rowc int

SELECT @rowc = @@rowcount
IF @rowc = 0
RETURN

SELECT @curyear = year(getdate()) * 10000

SELECT @curmax = coalesce(MAX(LabID) - @curyear, 0)
FROM nisse (HOLDLOCK)
WHERE LabID BETWEEN @curyear + 1 AND @curyear + 9999

IF @curmax + @rowc 10000
BEGIN
ROLLBACK TRANSACTION
RAISERROR ('%d rows have already been inserted this year, so you cannot
insert %d rows now. Please wait until next year',
16, 1, @curmax, @rowc)
RETURN
END

UPDATE nisse
SET LabID = @curyear + @curmax + i.newlabid
FROM nisse n
JOIN (SELECT a, newlabid = row_number() over (ORDER BY a)
FROM inserted) AS i ON n.a = i.a
go
INSERT nisse (a)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (a)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 18 '06 #3

P: n/a
Thank you for the response. Please bear in mind that the LabID is the
identifiable key; it is the PK for the table. Also, I don't understand

why LabID = @curyear + @curmax + i.newlabid instead of @curyear +
(@curmax + 1). Finally, could you please explain to me why the UNION
statements are necessary?

Again, thank you very much.
im*******************@yahoo.com wrote:
Thank you for the response. Please bear in mind that the LabID is the
identifiable key; it is the PK for the table. Also, I don't understand
why LabID = @curyear + @curmax + i.newlabid instead of @curyear +
(@curmax + 1). Finally, could you please explain to me why the UNION
statements are necessary?

Again, thank you very much.
Erland Sommarskog wrote:
im*******************@yahoo.com (im*******************@yahoo.com) writes:
The table in the database has a field called LabID. That field is an
integer and consists of the year plus a counter. For example, the
first record of 2006 would be "20060001," the second record of 2006
would be "20060002" and so on. I'm trying to create an Insert trigger
that can generate the ID value when a new record is inserted, but I'm
not quite sure how to implement that trigger. Can anyone help?
The script below demonstrates. Note that it presumes that the table
already has an identifyable key, else there is no possibility to handle
multi-row inserts.

The solution presumes SQL 2005.

CREATE TABLE nisse (a int NOT NULL PRIMARY KEY,
LabID int NULL)
CREATE INDEX LabID_ix ON nisse(LabID)
go
CREATE TRIGGER nisse_tri ON nisse FOR INSERT AS
DECLARE @curmax int,
@curyear int,
@rowc int

SELECT @rowc = @@rowcount
IF @rowc = 0
RETURN

SELECT @curyear = year(getdate()) * 10000

SELECT @curmax = coalesce(MAX(LabID) - @curyear, 0)
FROM nisse (HOLDLOCK)
WHERE LabID BETWEEN @curyear + 1 AND @curyear + 9999

IF @curmax + @rowc 10000
BEGIN
ROLLBACK TRANSACTION
RAISERROR ('%d rows have already been inserted this year, so you cannot
insert %d rows now. Please wait until next year',
16, 1, @curmax, @rowc)
RETURN
END

UPDATE nisse
SET LabID = @curyear + @curmax + i.newlabid
FROM nisse n
JOIN (SELECT a, newlabid = row_number() over (ORDER BY a)
FROM inserted) AS i ON n.a = i.a
go
INSERT nisse (a)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (a)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 18 '06 #4

P: n/a
im*******************@yahoo.com (im*******************@yahoo.com) writes:
Thank you for the response. Please bear in mind that the LabID is the
identifiable key; it is the PK for the table.
In such case you cannot use an AFTER trigger. A primary key cannot be NULL,
so it must have a value when you insert the data. Furthermore, if there is
no other data to correlate, it's impossible to handle multi-row inserts.
And a trigger that would reqiure rows to be inserted one at a time, is
completely indefensible. This could have serious impact the day you
need to insert many rows.
Also, I don't understand why LabID = @curyear + @curmax + i.newlabid
instead of @curyear + (@curmax + 1).
If you insert 10 rows at the time, you cannot give them all the same
ID, can you?
Finally, could you please explain to me why the UNION statements are
necessary?
I wanted to test that the trigger actually works with multi-row inserts.
SELECT UNION is a way to achieve that without using a table to select
from.

I think it's possible to do this with INSTEAD OF trigger, but then I
need to know which version of SQL Server you are using.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 18 '06 #5

P: n/a
I'm using SQL Server 2005. By the way, the front-end app is an Access
Data Project.

Erland Sommarskog wrote:
im*******************@yahoo.com (im*******************@yahoo.com) writes:
Thank you for the response. Please bear in mind that the LabID is the
identifiable key; it is the PK for the table.

In such case you cannot use an AFTER trigger. A primary key cannot be NULL,
so it must have a value when you insert the data. Furthermore, if there is
no other data to correlate, it's impossible to handle multi-row inserts.
And a trigger that would reqiure rows to be inserted one at a time, is
completely indefensible. This could have serious impact the day you
need to insert many rows.
Also, I don't understand why LabID = @curyear + @curmax + i.newlabid
instead of @curyear + (@curmax + 1).

If you insert 10 rows at the time, you cannot give them all the same
ID, can you?
Finally, could you please explain to me why the UNION statements are
necessary?

I wanted to test that the trigger actually works with multi-row inserts.
SELECT UNION is a way to achieve that without using a table to select
from.

I think it's possible to do this with INSTEAD OF trigger, but then I
need to know which version of SQL Server you are using.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 18 '06 #6

P: n/a
im*******************@yahoo.com (im*******************@yahoo.com) writes:
I'm using SQL Server 2005. By the way, the front-end app is an Access
Data Project.
Ok, here is a revised and tested version using an INSTEAD OF trigger:

CREATE TABLE nisse (LabID int NOT NULL PRIMARY KEY,
somemoredata int NULL)
go
CREATE TRIGGER nisse_tri ON nisse INSTEAD OF INSERT AS
DECLARE @curmax int,
@curyear int,
@rowc int

SELECT @rowc = @@rowcount
IF @rowc = 0
RETURN

SELECT @curyear = year(getdate()) * 10000

SELECT @curmax = coalesce(MAX(LabID) - @curyear, 0)
FROM nisse (HOLDLOCK)
WHERE LabID BETWEEN @curyear + 1 AND @curyear + 9999

IF @curmax + @rowc 10000
BEGIN
ROLLBACK TRANSACTION
RAISERROR ('%d rows have already been inserted this year, so you cannot insert %d rows now. Please wait until next year',
16, 1, @curmax, @rowc)
RETURN
END

INSERT nisse(LabID, somemoredata)
SELECT @curyear + @curmax + row_number() OVER (ORDER BY newid()),
somemoredata
FROM inserted
go
INSERT nisse (somemoredata)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (somemoredata)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 19 '06 #7

P: n/a
BLESS YOU!!! I'll look at this immediately.

Erland Sommarskog wrote:
im*******************@yahoo.com (im*******************@yahoo.com) writes:
I'm using SQL Server 2005. By the way, the front-end app is an Access
Data Project.

Ok, here is a revised and tested version using an INSTEAD OF trigger:

CREATE TABLE nisse (LabID int NOT NULL PRIMARY KEY,
somemoredata int NULL)
go
CREATE TRIGGER nisse_tri ON nisse INSTEAD OF INSERT AS
DECLARE @curmax int,
@curyear int,
@rowc int

SELECT @rowc = @@rowcount
IF @rowc = 0
RETURN

SELECT @curyear = year(getdate()) * 10000

SELECT @curmax = coalesce(MAX(LabID) - @curyear, 0)
FROM nisse (HOLDLOCK)
WHERE LabID BETWEEN @curyear + 1 AND @curyear + 9999

IF @curmax + @rowc 10000
BEGIN
ROLLBACK TRANSACTION
RAISERROR ('%d rows have already been inserted this year, so you cannot insert %d rows now. Please wait until next year',
16, 1, @curmax, @rowc)
RETURN
END

INSERT nisse(LabID, somemoredata)
SELECT @curyear + @curmax + row_number() OVER (ORDER BY newid()),
somemoredata
FROM inserted
go
INSERT nisse (somemoredata)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (somemoredata)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 21 '06 #8

P: n/a
Again, thank you very much. Am I right to assume that the following
section is unnecessary, just for testing?
INSERT nisse (somemoredata)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (somemoredata)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse
Sep 21 '06 #9

P: n/a
im*******************@yahoo.com (im*******************@yahoo.com) writes:
Again, thank you very much. Am I right to assume that the following
section is unnecessary, just for testing?
>INSERT nisse (somemoredata)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (somemoredata)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse
Well, testing is never unnecessary. :-)

But you are right that this is not part of the solution.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 21 '06 #10

P: n/a
Speaking of testing, what would happen if I inserted a bunch of rows
into the table that already have LabID's? Would that cause a problem
with the trigger? I'm thinking about future data migration.

Erland Sommarskog wrote:
im*******************@yahoo.com (im*******************@yahoo.com) writes:
Again, thank you very much. Am I right to assume that the following
section is unnecessary, just for testing?
INSERT nisse (somemoredata)
SELECT 8
UNION ALL SELECT 188
UNION ALL SELECT 234
go
select * from nisse
INSERT nisse (somemoredata)
SELECT 18
UNION ALL SELECT 5188
UNION ALL SELECT 9234
go
select * from nisse
drop table nisse

Well, testing is never unnecessary. :-)

But you are right that this is not part of the solution.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 21 '06 #11

P: n/a
im*******************@yahoo.com (im*******************@yahoo.com) writes:
Speaking of testing, what would happen if I inserted a bunch of rows
into the table that already have LabID's? Would that cause a problem
with the trigger? I'm thinking about future data migration.
The trigger does not take that in regard, because that was not in the
requirement specification. But you could use the coalesce function to
handle this. coalesce takes a list of values as input parameters, and
returns the first non-NULL value in the list.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 22 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.