当列名是变量时,从INSERT动态查找触发器中的列值

时间:2009-06-02 15:22:19

标签: sql-server sql-server-2008 triggers dynamic-sql

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

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

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

一个问题是,如果我使用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的值应为null,并且我继续遍历可能的列名称,直到我完成,然后处理触发器。

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

CREATE TRIGGER TempTableAttr_LotTrigger 
ON  TempTableAttr_Lot 
INSTEAD OF UPDATE, INSERT

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

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

2 个答案:

答案 0 :(得分:3)

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

SELECT * INTO #MYINSERTED FROM INSERTED

EXEC('SELECT * FROM #MYINSERTED')

DROP TABLE #MYINSERTED

答案 1 :(得分: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;