简体   繁体   中英

Get deleted values from 3 tables and put in 1 table

I have three tables 这是数据库模型

The end-user can select a top2000jaar from a dropdownlist in ASP, which will has its value send to an stored procedure, the stored procedure has to delete all the values in [LIJST] where top2000jaar equals the value that has been selected but a trigger is needed which delete the row where song.songid = lijst.songid from [SONG] when that songid doesnt exist in [LIJST] anymore, and therefore if an artist in [artiest] has no more songs left in [song], the artist should be deleted too.

All that deleted data should be then put in [logtable]

样本数据

I have tried a way which i thought would work, but didnt.

Expected output should be this:

<- 31-dec-13 is a typo, it is 2013

This is the trigger is created, with some answers i found on google, but not working:

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

I believe using this as your trigger should do the job, this will only populate what has actually been deleted so if artiest has not been deleted it will show as null next to the song etc.

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

Here is an example SQLFiddle where song 9 is deleted and it's the last song for that artist: http://sqlfiddle.com/#!6/4405b/1/0

The following will show all fields on deletion:

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

Here is the SQLFiddle for this: http://sqlfiddle.com/#!6/fd6171/1/0

You could just add the logic to the stored procedure to do all of that, something like this:

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 simply returns the number of rows affected by the previous statement, so it will only log if a row is deleted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM