我有一个插入触发器来插入表的主键(varchar)。我想获取最后插入的主键值。这是我的插入触发器
ALTER TRIGGER dbo.equipment_insert_pk
ON dbo.Equipment
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
declare @id int;
declare @cat varchar(2);
select @cat=CategoryID from INSERTED;
select @id=cast(max(right(EquipmentID,4)) as int) from Equipment where CategoryID=@cat;
set @id=isnull(@id,0)+1;
-- Insert statements for trigger here
insert into Equipment
select CategoryID+right('0000'+cast(@id as varchar(4)),4)
,CategoryID
,Location
,Detail
,'../BarcodeImage/'+CategoryID+right('0000'+cast(@id as varchar(4)),4)+'.jpeg' from INSERTED;
END
我试图编写一个函数来获取最后插入的主键值,但它似乎不起作用..因为触发器之外没有“插入”的范围。帮助我..我在触发器中尝试了这段代码,输出子句没有结果所以远的..
insert into Equipment
output INSERTED.EquipmentID into @Temp_Tbl
select CategoryID+right('0000'+cast(@id as varchar(4)),4)
,CategoryID
,Location
,Detail
,'../BarcodeImage/'+CategoryID+right('0000'+cast(@id as varchar(4)),4)+'.jpeg' from INSERTED;
有什么帮助吗??