I'm trying to select from a table where the primary_no (VARCHAR(20)) does not include any of the strings in the @IDS variable, but it's not working and I have tried using a CTE, not exists and not in. Neither of them work. The query still selects all of the data even those with the primary_no that are in the @IDS variable.
ALTER PROCEDURE [dbo].[LinkProc] @IDS VARCHAR(MAX) /* DECLARE @IDS VARCHAR(MAX) SET @IDS = ' ''00000447'', ''0000300'', ''2900071'', ''2900192'' ' EXEC LinkProc @IDS = @IDS */ AS WITH cte (id) AS ( SELECT * FROM dbo.splitstring(@IDS) ) SELECT * FROM link_tb WHERE grp_id = 4 AND status_cd = 'A' AND primary_no NOT IN (SELECT id FROM cte) I have also tried this with no luck:
SELECT * FROM link_tb lt WHERE grp_id = 4 AND status_cd = 'A' AND NOT EXISTS (SELECT id FROM cte c WHERE lt.primary_no = c.id) The result set from calling (SELECT id FROM cte):
'00000447' '0000300' '2900071' '2900192' I found a solution to this problem, I answered below.