SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[ListToTable] ( @mylist varchar(8000) ) RETURNS @ListTable TABLE ( seqid int not null, entry varchar(255) not null) AS BEGIN DECLARE @this varchar(255), @rest varchar(8000), @pos int, @seqid int SET @this = ' ' SET @seqid = 1 SET @rest = @mylist SET @pos = PATINDEX('%,%', @rest) WHILE (@pos > 0) BEGIN set @this=substring(@rest,1,@pos-1) set @rest=substring(@rest,@pos+1,len(@rest) -@pos ) INSERT INTO @ListTable (seqid,entry) VALUES (@seqid,@this) SET @pos= PATINDEX('%,%', @rest) SET @ seqid=@seqid +1 END set @ this=@rest INSERT INTO @ListTable (seqid,entry) VALUES (@seqid,@this) RETURN END
Run the script in the SQL Server database to create the ListToTable function. Now you can rewrite your request like this:
@attributetypeid bigint, @productid bigint, @includedids varchar(MAX) DELETE FROM reltable WHERE productid = @productid AND attributetypeid = @attributetypeid AND attributeid NOT IN (SELECT entry FROM ListToTable(@includedids));
Where @includedids is the comma separated list you provide. I use this function all the time when working with lists. Keep in mind that this function does not necessarily sanitize your inputs; it simply searches for character data in a comma-separated list and puts each item in the entry. Hope this helps.
karlgrz
source share