使用触发器生成ID值 [英] Using a trigger to generate an ID value

查看:95
本文介绍了使用触发器生成ID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库中的表有一个名为LabID的字段。该字段是一个

整数,由年份和计数器组成。例如,2006年的第一个记录将是20060001,20060001。 2006年的第二个记录

将是20060002。等等。我正在尝试创建一个插入触发器

,可以在插入新记录时生成ID值,但是我不知道如何实现
那个触发器。任何人都可以帮忙吗?

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?

推荐答案

im ******************* @ yahoo.com (im ******** ***********@yahoo.com)写道:
im*******************@yahoo.com (im*******************@yahoo.com) writes:

数据库中的表有一个名为LabID的字段。该字段是一个

整数,由年份和计数器组成。例如,2006年的第一个记录将是20060001,20060001。 2006年的第二个记录

将是20060002。等等。我正在尝试创建一个插入触发器

,可以在插入新记录时生成ID值,但是我不知道如何实现
那个触发器。有人可以帮忙吗?
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?



下面的脚本演示了。请注意,它假设表

已经有一个可识别的键,否则就不可能处理

多行插入。


解决方案假定SQL 2005.


CREATE TABLE nisse(一个int NOT NULL主键,

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

返回


SELECT @curyear =年(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行已经插入今年,所以你不能

现在插入%d行。请等到明年'',

16,1,@curmax,@ ruc)

返回

结束


UPDATE nisse

SET LabID = @curyear + @curmax + i.newlabid

FROM nisse n

JOIN(SELECT a,newlabid = row_number()over(ORDER BY a)

FROM FROM)AS i ON na = ia

go

INSERT nisse( a)

SELECT 8

UNION ALL SELECT 188

UNION ALL SELECT 234

go

选择*来自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


SQL Server 2005联机丛书
HT tp://www.microsoft.com/technet/pro...ads/books.mspx

SQL Server 2000联机丛书
http://www.microsoft.com/sql/prodinf...ons/books。 mspx

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


感谢您的回复。请记住,LabID是

可识别密钥;它是桌子的PK。另外,我不明白

为什么LabID = @curyear + @curmax + i.newlabid而不是@curyear +

(@curmax + 1)。最后,您能否向我解释为什么需要UNION

的陈述?


再次,非常感谢。

Erland Sommarskog写道:
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)写道:
im*******************@yahoo.com (im*******************@yahoo.com) writes:

数据库中的表有一个名为LabID的字段。该字段是一个

整数,由年份和计数器组成。例如,2006年的第一个记录将是20060001,20060001。 2006年的第二个记录

将是20060002。等等。我正在尝试创建一个插入触发器

,可以在插入新记录时生成ID值,但是我不知道如何实现
那个触发器。有人可以帮忙吗?
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?



下面的脚本演示了。请注意,它假设表

已经有一个可识别的键,否则就不可能处理

多行插入。


解决方案假定SQL 2005.


CREATE TABLE nisse(一个int NOT NULL主键,

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

返回


SELECT @curyear =年(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行已经蜜蜂了n今年插入,所以你不能现在插入%d行
。请等到明年'',

16,1,@curmax,@ ruc)

返回

结束


UPDATE nisse

SET LabID = @curyear + @curmax + i.newlabid

FROM nisse n

JOIN(SELECT a,newlabid = row_number()over(ORDER BY a)

FROM FROM)AS i ON na = ia

go

INSERT nisse( a)

SELECT 8

UNION ALL SELECT 188

UNION ALL SELECT 234

go

选择*来自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


SQL Server 2005联机丛书
http://www.microsoft.com/technet/pro...ads/books.mspx

书籍在线获取SQL Server 2000
http:// www.microsoft.com/sql/prodinf...ons/books.mspx


感谢您的回复。请记住,LabID是

可识别密钥;它是桌子的PK。另外,我不明白


为什么LabID = @curyear + @curmax + i.newlabid而不是@curyear +

(@curmax + 1 )。最后,您能否向我解释为什么UNION

陈述是必要的?


再次,非常感谢。
IM ******************* @ yahoo.com 写道:
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:

感谢您的回复。请记住,LabID是

可识别密钥;它是桌子的PK。另外,我不明白

为什么LabID = @curyear + @curmax + i.newlabid而不是@curyear +

(@curmax + 1)。最后,您能否向我解释为什么需要UNION

的陈述?


再次,非常感谢。


Erland Sommarskog写道:
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)写道:
im*******************@yahoo.com (im*******************@yahoo.com) writes:

数据库中的表有一个名为LabID的字段。该字段是一个

整数,由年份和计数器组成。例如,2006年的第一个记录将是20060001,20060001。 2006年的第二个记录

将是20060002。等等。我正在尝试创建一个插入触发器

,可以在插入新记录时生成ID值,但是我不知道如何实现
那个触发器。有人可以帮忙吗?
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?



下面的脚本演示了。请注意,它假设表

已经有一个可识别的键,否则就不可能处理

多行插入。


解决方案假定SQL 2005.


CREATE TABLE nisse(一个int NOT NULL主键,

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

返回


SELECT @curyear =年(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行已在今年现在插入,所以你不能

插入%d行。请等到明年'',

16,1,@curmax,@ ruc)

返回

结束


UPDATE nisse

SET LabID = @curyear + @curmax + i.newlabid

FROM nisse n

JOIN(SELECT a,newlabid = row_number()over(ORDER BY a)

FROM FROM)AS i ON na = ia

go

INSERT nisse( a)

SELECT 8

UNION ALL SELECT 188

UNION ALL SELECT 234

go

选择*来自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


SQL Server 2005联机丛书
http://www.microsoft.com/technet/pro...ads/books.mspx

SQL Server 2000联机丛书
http://www.microsoft.com/sql/prodinf...ons/books.mspx


这篇关于使用触发器生成ID值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆