Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Delete records that don't match

Author  Topic 

luke.wilkinson
Starting Member

4 Posts

Posted - 2011-12-08 : 03:19:18
I have a linker table that has two columns:

SongID & ArtistID

I can insert new records to this by a stored procedure that has @SongID and @ArtistID. The store procedure is being called from a c# codebehind in a loop of Artists but is only ever the one songID. So I can just check if the ArtistID exists agains the SongID and if not I can insert.

But I also need to delete any records that do not match the artisits in the loop. I have thought I could had a modfied date column to the table then just update that for every record in the loop then delete any that the date is older than say 10mins. But I don't feel this is the best way. Can anyone suggest a better way of doing this in SQL?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-08 : 06:58:18
you mean delete all records for other artists which are not considered in current loop? whats that for? doesnt sound like a good approach to me

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

luke.wilkinson
Starting Member

4 Posts

Posted - 2011-12-08 : 07:16:37
Delete all records that song no longer is associated with that artist. So EG. SoongID = 1 and is currently connected to artist 1, 2 and 3. Later the song has been updated so it now has artist 4 but artist 1 needs to be removed.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-08 : 07:36:56
ok for that whay you can do is

DELETE FROM LinkTable WHERE SongID=@SongID AND ArtistID <> @ArtistID

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

luke.wilkinson
Starting Member

4 Posts

Posted - 2011-12-08 : 08:09:03
The problem with that is it's only checking one artistID. The stored procedure is being called in a loop so ther could be 3 artistID's against that songID but when you check it you are only checking it against one of the artistID's?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-08 : 08:48:56
quote:
Originally posted by luke.wilkinson

The problem with that is it's only checking one artistID. The stored procedure is being called in a loop so ther could be 3 artistID's against that songID but when you check it you are only checking it against one of the artistID's?


ok then put the @artistIDs inside a table variable inside loop @Artists and then after loop do following

DELETE l
FROM LinkTable l
LEFT JOIN @Artists a
ON a.ArtistID = iArtistID
WHERE l.SongID=@SongID
AND a.ArtistID IS NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

luke.wilkinson
Starting Member

4 Posts

Posted - 2011-12-08 : 09:58:15
I looked at your suggestion but I am using SQL 2005 and you can't use table varibles as parameters in 2005... :(
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-08 : 10:03:22
why should you pass parameter table variable? you can pass them as comma separated list and then use a parsing udf like below to get them on a table

http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -