当Value插入另一个表中的任何列时,使用Trigger插入值

时间:2015-03-12 02:01:58

标签: sql-server triggers

我有一个GenreTable:

enter image description here

我有一个“CombinedTable”:

enter image description here

我有一个触发器:

ALTER TRIGGER [dbo].[trgAfterUpdate] on [dbo].[Genre]
AFTER UPDATE
AS
   INSERT INTO [dbo].[CombinedTags]([Combined])

   SELECT TOP 1 PodcastGenre
   FROM Genre
    ORDER BY rowID DESC

我正在尝试触发器,以便当一个值在“GenreTable”[(MovieGenre),(MusicGenre),(PodcastGenre)]中的列的以太符号时

它将“RowID”插入“CombinedTable”的“ForeignRowID”列中,并将“value”插入“GenreTable”列的以太列中,插入“Combined”。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

AS
declare @rowid int


   declare @insfld varchar(max)

    select @rowid = rowid from inserted
-- assume that only one field has value....
    select @insfld = case when moviegenre is not null then moviegenre
                        when musicgenre is not null then musicgenre
                          else podcastgenre end whichone
                from inserted
       INSERT INTO [dbo].[CombinedTags]([Combined], foreignkeyid)  values
           ( @insfld,@rowid)