触发器、游标

1.

--循环遍历每一个版本,如果没有这个质量要求则将这个质量要求插入进去 

declare CUR_pbi cursor for 
SELECT pbi,pbiname 
FROM MANAGER_VERSION_CONFIG 
open CUR_pbi 
fetch next from CUR_pbi into @pbi,@pbiname --将游标向下移1行,获取的数据放入之前定义的变量@versionpbi中 
while @@fetch_status=0 ---循环判断游标还在读取 
begin 
SELECT @count = count(*) FROM dbo.MANAGER_VERSION_lINK WHERE ipalid = @id AND pbi = @pbi 
if @count = 0 
begin
INSERT INTO dbo.MANAGER_VERSION_lINK 
([pbi], 
[pbiname], 
[ipalid], 
[userid], 
[addtime], 
[pduid] 

SELECT @pbi, 
@pbiname, 
[Id], 
@userid, 
@addtime,
[SourceArea]
FROM INSERTED 

update MANAGER_DATA_IPAL set linktimes =linktimes + 1 where Id = @id 
end 
fetch next from CUR_pbi into @pbi,@pbiname 
end
close CUR_pbi --关闭游标 
deallocate CUR_pbi ----删除游标

2.

ALTER TRIGGER [dbo].[MANAGER_DATA_IPAL_INSERT] 
ON [dbo].[MANAGER_DATA_IPAL] 
AFTER INSERT 
AS 
BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 

-- Insert statements for trigger here 
DECLARE @id int, 
@status VARCHAR(50), 
@isbaseline numeric(1,0), 
@userid VARCHAR(500), 
@addtime datetime, 
@pbi VARCHAR(100), 
@pbiname VARCHAR(1000), 
@count int, 
@pduid VARCHAR(50), 
@userid1 VARCHAR(1000), 
@bigtechname VARCHAR(50) 

SELECT @id = [Id], @status = Status, @isbaseline = IsBaseLine,@userid=[OperAuthorNotesId],@addtime=(getdate()),@pduid=SourceArea,@bigtechname = [BigTechName] FROM inserted 
if(@status = 'PASS' and @isbaseline = 1) 
begin 
--循环遍历每一个版本,如果没有这个质量要求则将这个质量要求插入进去 
declare CUR_pbi1 cursor for 
SELECT pbi,pbiname 
FROM MANAGER_VERSION_CONFIG 
open CUR_pbi1 
fetch next from CUR_pbi1 into @pbi,@pbiname --将游标向下移1行,获取的数据放入之前定义的变量@versionpbi中 
while @@fetch_status=0 
begin 
SELECT @count = count(*) FROM dbo.MANAGER_VERSION_lINK WHERE ipalid = @id AND pbi = @pbi 
if @count = 0 
begin
INSERT INTO dbo.MANAGER_VERSION_lINK 
([pbi], 
[pbiname], 
[ipalid], 
[userid], 
[addtime], 
[pduid] 

SELECT @pbi, 
@pbiname, 
[Id], 
@userid, 
@addtime,
[SourceArea]
FROM INSERTED 

update MANAGER_DATA_IPAL set linktimes =linktimes + 1 where Id = @id 

end 
fetch next from CUR_pbi1 into @pbi,@pbiname 
end
close CUR_pbi1 --关闭游标 
deallocate CUR_pbi1 
end 

-- ============================================= 
-- Author: <CHENWEI> 
-- Create date: <2011-5-24> 
-- Description: <增加资产的同时,记录动作到MANAGER_DATALOG_IPAL表中> 
-- ============================================= 
--insert into MANAGER_DATALOG_IPAL values (@Id,'CHECKED',@userid,getdate(),'',0,getdate()) 

select top 1 @userid1 = a.userid from MANAGER_CFG_CATEGORY a left join MANAGER_CFG_CATEGORY b on a.ParentId = b.Id 
where b.ParentId = 0 and b.TypeName = '领域类别' and b.PduId = Convert(int,Round(@pduid,0)) and a.CateName=@bigtechname 

--判断是否是领主 
if patindex('%'+@userid+'%',@userid1) = 0 --返回指定表达式中某模式第一次出现的起始位置;如果在全部有效的文本和字符数据类型中没有找到该模式,则返回零 
begin 
insert into MANAGER_DATALOG_IPAL values (@Id,'CHECKED',@userid,getdate(),'',0,getdate()) 
end 
else 
begin 
insert into MANAGER_DATALOG_IPAL values (@Id,'PASS',@userid,getdate(),'',0,getdate()) 
end 

END

你可能感兴趣的:(触发器)