日期:2014-05-18  浏览次数:20577 次

求教 ,触发器 和newid()问题
我通过一张表unique_index_forAll 用newid()的方式产生一个唯一标志(uniqueidentifier类型),然后做个触发器,当插入unique_index_forAll,会产生一个唯一标志。然后将这个标志取出 存在另外一张表GZL_LY_MB中 产生一个字符串唯一标志做为主键使用。
但是我这样做的话 开头3行 可以正确触发事件,2张表中都正确生成了主键唯一标志,但是多插入几行后 会提示PK_GZL_LY_MB有重复的错误 不知道该怎么解决。希望高手赐教
 
由于在外部程序中uniqueidentifier不能被识别为字符串,只有将他在sqlserver中转换为字符串存储,

触发器如下:
--产生唯一的标志
CREATE TRIGGER create_unique ON [dbo].[unique_index_forAll] 
FOR INSERT
AS
declare @unique_index char(60)
select @unique_index = uniqueIndex from unique_index_forAll
insert into GZL_LY_MB values(@unique_index,'','')




------解决方案--------------------
--存储过程
create proc Test(@ID uniqueidentifier output,@Name nvarchar(50))
as
set @ID=newID()
insert Test values(@ID,@Name)

go
declare @ID uniqueidentifier
exec Test ID output,@Name

insert T2 values(@ID)