日期:2014-05-17 浏览次数:20504 次
Create Procedure usp_CreateTrigger As Begin Declare @Tbl Table (TblName Varchar(100)) --获取表命 Insert Into @Tbl Select SUBSTRING(Name, 1, CHARINDEX('_', Name) - 1) From Sysobjects Where Name Like '%_Master' Declare @tName varchar(100), @TB_Master varchar(128), @TB_Detail Varchar(128), @SQL Varchar(8000) Declare tItem cursor for SELECT TblName From @Tbl Open tItem FETCH NEXT FROM tItem into @tName WHILE (@@FETCH_STATUS <> -1) and (@@FETCH_STATUS <> -2) BEGIN Set @TB_Master = @tName + '_Master' Set @TB_Detail = @tName + '_Detail' Set @SQL = 'Create Trigger tru_'+@TB_Master+'_Flag On '+@TB_Master+' for Update As if Update(Flag) begin Update A Set A.Flag = B.Flag From '+@TB_Detail+' A ,Inserted B Where A.NO=B.No end ' Print @SQL Exec(@SQL) FETCH NEXT FROM tItem into @tName END CLOSE tItem DEALLOCATE tItem End