2

我有一个触发器,可以插入和更新视图。我需要找到正在修改的列并将值放入正确的表中。

INSERT INTO TempTableAttr_Lot(ID, [% Num]) VALUES(3, 24.0)

我试图弄清楚如何在我的触发器中获取 ID 和 [% Num] 列的值。问题是在插入或更新中可以设置 32 个不同的列,所以我想循环查看该列是否在“插入”表中。

一个问题是,如果我使用 exec 或 execute 来构建动态查询,则 insert 不在此范围内。

该视图是动态的,因为如果添加了属性,则该视图将由存储过程重新生成,因此我不能假设在任何给定时间视图中有哪些列名,它可以增长和缩小。

理想情况下我想做SET @Value = (SELECT i.[@Name] FROM inserted i)但是@Name不是列名,而是触发器中的一个变量,所以当我尝试

DECLARE @ValueTable TABLE ( value sql_variant)
INSERT INTO @ValueTable EXECUTE('SELECT i.[' + @Name + '] FROM inserted i')
SET @Value = CONVERT(nvarchar(128), (SELECT DISTINCT * FROM @ValueTable))

这不起作用,因为插入的似乎不在范围内。

对于第一个循环 @Name = 'ID' 和第二个循环将是 'Col2' 没有插入,所以 @Value 的值应该为空,我继续循环遍历可能的列名,直到我完成,并且触发器被处理。

我称它为触发器,它被定义为:

CREATE TRIGGER TempTableAttr_LotTrigger 
ON  TempTableAttr_Lot 
INSTEAD OF UPDATE, INSERT

我目前正在使用 INSERT 命令进行测试。我意识到在进行更新时我可能需要查看“已删除”表。

更新:我假设目前只插入一行,用于我的测试,看看如何让它工作。这是一个实体-属性-值数据库,但我有一个使它看起来有关系的视图。使用集成服务时,更新正在进行时,对视图的任何插入或更新都会调用触发器。我只是想了解如何获取我需要的信息,以便我可以使用正确的值更新正确的表。

4

2 回答 2

3

要访问 INSERTED 表,您可以执行以下操作。

SELECT * INTO #MYINSERTED FROM INSERTED

EXEC('SELECT * FROM #MYINSERTED')

DROP TABLE #MYINSERTED
于 2009-06-02T15:33:22.137 回答
0
CREATE TRIGGER  triggerRowLog_tblMember 
--After INSERT  
ON dbo.tblMember
 AFTER UPDATE
--for EACH ROW
As
BEGIN

DECLARE @temptblCategory TABLE ( 
        idx smallint Primary Key IDENTITY(1,1)
        , CategoryID int
        , CategoryName nvarchar(100)
    )
    declare @i int
    set @i = 1
    declare @catcount int 
    set @catcount = 0
    Select @catcount=  Count(*) from tblCategoryMst Where Tag = 'A' or Tag = 'L'  and Active =1
    declare @CatName nvarchar(100)
    declare @CatID int
    declare @ValueCompare nvarchar(100)
    declare @PrevValueCompare nvarchar(100)
if(@catcount > 0)
begin
INSERT @temptblCategory 
    SELECT distinct CategoryID, CategoryName FROM tblCategoryMst  Where (Tag = 'A' or Tag = 'L' )and (TableName = 'Transactions' or TableName ='tblMember')  and Active =1
    While @i <= @catcount
        Begin


            set @CatName = (SELECT CategoryName FROM @temptblCategory WHERE idx = @i)
            set @CatID = (SELECT CategoryID FROM @temptblCategory WHERE idx = @i)

            if(@CatName = 'Form')
            Begin
                set @CatName = @CatName + 'Member'
            End
            else if(@CatName = 'Type')
            Begin
                set @CatName = 'Membership' + @CatName 
            End
            else if(@CatName = 'Address')
            Begin
                set @CatName = 'Cor' + @CatName 
            End

            declare @Query nvarchar(4000)
            SELECT * INTO #MYInserted FROM Inserted
            SELECT * INTO #MYDeleted FROM Deleted
            set @Query ='(Select @PrevValueCompare = ' + @CatName+' from #MYDeleted )'
            exec sp_executesql @Query ,N'@PrevValueCompare nvarchar(50) output', @PrevValueCompare output
            set @Query ='(Select @ValueCompare = ' + @CatName+' from #MYInserted )'
            exec sp_executesql @Query ,N'@ValueCompare nvarchar(50) output', @ValueCompare output

            DROP TABLE #MYInserted
            DROP TABLE #MYDeleted

            if( @ValueCompare <> 'no compare' and @ValueCompare  <>  @PrevValueCompare)
            Begin
                INSERT INTO dbo.tblLogMst(ID,IDOf,CategoryID, PreviousData, ChangedData,
                    ExchangeCode,CreatedBy,CreatedIP )
                      SELECT i.MemberID,'MS',@CatID, @PrevValueCompare, @ValueCompare ,
                    i.ExchangeCode,i.UpdatedBy,i.UpdatedIP
                      FROM Inserted i
                      INNER JOIN Deleted d ON i.MemberID = d.MemberID
            End
        set @i =  Convert(int,@i) + Convert(int,1)
        End
    end
END;
于 2012-06-07T12:14:07.067 回答