从3个表中获取已删除的值并放入1个表中

时间:2015-06-10 10:27:46

标签: sql asp.net sql-server triggers

我有三张表This is the database model

最终用户可以从ASP中的下拉列表中选择一个top2000jaar,它的值将发送到存储过程,存储过程必须删除[LIJST]中所有值,其中top2000jaar等于已选择的值但需要一个触发器,当[LIJST]中不再存在该歌曲时,从[SONG]删除song.songid = lijst.songid的行,因此如果[artiest]中的艺术家在[song]中没有剩余的歌曲,艺术家也应该被删除。

然后应将所有删除的数据放入[logtable]

sample data

我尝试了一种我认为可行的方法,但没有。

预期输出应为:

< - 31-dec-13是一个错字,是2013年

这是创建的触发器,我在谷歌找到了一些答案,但没有工作:

ALTER TRIGGER [dbo].[TRGremoveSong]
ON [dbo].[Lijst]
AFTER DELETE AS

Begin
DELETE FROM song where songid not in (Select songid from lijst)
END
begin
INSERT INTO logtable
   SELECT 
       *
   FROM deleted
 end

2 个答案:

答案 0 :(得分:1)

我相信使用它作为你的触发器应该完成这项工作,这只会填充实际删除的内容,所以如果没有删除它,它将在歌曲等旁边显示为null。

ALTER TRIGGER [dbo].[TRGremoveSong]
ON [dbo].[Lijst]
AFTER DELETE AS

Begin
   INSERT INTO logtable
   SELECT  sng.titel, art.naam, sng.jaar, positie, top2000jaar
   FROM deleted d
   Outer Apply
   (Select titel, jaar, artiestid from song where not exists (Select * from lijst where songid = d.songid) and songid = d.songid) sng
   Outer Apply
   (Select naam from artiest where not exists (Select * from song where artiestid = sng.artiestid and songid <> d.songid) and artiestid = sng.artiestid) art

DELETE FROM song s where not exists (Select * from lijst where songid = s.songid)
DELETE FROM artiest a where not exists (Select * from song where artiestid = a.artiestid)
End

这是一个示例SQLFiddle,其中歌曲9被删除,它是该艺术家的最后一首歌:http://sqlfiddle.com/#!6/4405b/1/0

以下内容将显示删除的所有字段:

ALTER TRIGGER [dbo].[TRGremoveSong]
ON [dbo].[Lijst]
AFTER DELETE AS

Begin
   INSERT INTO logtable
   SELECT  sng.titel, art.naam, sng.jaar, positie, top2000jaar
   FROM deleted d
   Outer Apply
   (Select titel, jaar, artiestid from song where songid = d.songid) sng
   Outer Apply
   (Select naam from artiest where artiestid = sng.artiestid) art

DELETE FROM song s where not exists (Select * from lijst where songid = s.songid)
DELETE FROM artiest a where not exists (Select * from song where artiestid = a.artiestid)
End

以下是SQLFiddle:http://sqlfiddle.com/#!6/fd6171/1/0

答案 1 :(得分:0)

您只需将逻辑添加到存储过程即可完成所有这些操作,如下所示:

DECLARE @LIJSTID_TO_DELETE INT = 123
DECLARE @SongID INT
DECLARE @ArtistID INT

-- SET the songid for the row you're going to delete
SELECT @SongID = songid 
FROM LIJST 
WHERE top2000jaar = @LIJSTID_TO_DELETE

-- first deletion
DELETE FROM LIJST 
WHERE top2000jaar = @LIJSTID_TO_DELETE

IF @@ROWCOUNT > 0
    INSERT INTO LOGTABLE.... -- deleted LIJST values

-- set the artist id for the song you are going to delete
SELECT @ArtistID = artiestid
FROM Song 
WHERE songid = @SongID

-- delete the linked song (could use cascade delete if required)
DELETE FROM Song
WHERE songid = @SongID

IF @@ROWCOUNT > 0
    INSERT INTO LOGTABLE.... -- deleted song values

-- Delete the artist if their songs no longer exist
IF NOT EXISTS (SELECT * FROM Song WHERE artiestid = ArtistID )
BEGIN
    DELETE FROM Artiest
    WHERE artiestid = @ArtistID 

    IF @@ROWCOUNT > 0
        INSERT INTO LOGTABLE.... -- deleted artiest values
END

@@ROWCOUNT只返回受前一个语句影响的行数,因此只有在删除行时才会记录。