--Create sample table
CREATE TABLE Test(ID INT IDENTITY(1,1), Column1 VARCHAR(30))
INSERT INTO Test (Column1) VALUES ('y');
INSERT INTO Test (Column1) VALUES ('xyz');
INSERT INTO Test (Column1) VALUES ('i');
--Parameter being passed
DECLARE @Parameter VARCHAR(MAX) = 'y,i,for,xyz,abc'
--===== Add start and end commas to the Parameter so we can handle single elements
SET @Parameter = ','+@Parameter +','
--Normally, I'd have this table predefined, but you can generate it on the fly
;WITH
D1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
D2(N) AS (SELECT 1 FROM D1 a, D1 b),
D4(N) AS (SELECT 1 FROM D2 a, D2 b)
, Numbers AS (
SELECT TOP (1000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number FROM D4
)
--Numbers contains a list of numbers from 1-1000. Will handle up to 1000 items in the delimited list.
-- Adjust larger or smaller depending upon your needs
--Join the Numbers table to the string at the character level and
-- when we find a comma, select what's between that and the next comma
, ParsedData AS (
SELECT SUBSTRING(@Parameter,N.Number+1,CHARINDEX(',',@Parameter,N.Number+1)-N.Number-1) AS Data
FROM Numbers n
WHERE N.Number < LEN(@Parameter)
AND SUBSTRING(@Parameter,N.Number,1) = ','
)
SELECT * FROM ParsedData
WHERE DATA NOT IN (SELECT Column1 FROM Test)
DECLARE @x AS XML=''
DECLARE @Param AS VARCHAR(100) = 'y,i,xyz'
SET @x = CAST('<A>'+ REPLACE(@Param,',','</A><A>')+ '</A>' AS XML)
SELECT * FROM
(
SELECT t.value('.', 'VARCHAR(10)') Value FROM @x.nodes('/A') AS x(t)
)k
OUTER APPLY
(
SELECT * From yourtable b
WHERE b.[columne] = k.value
)
WHERE k.Value IS NULL
I have Access app linked to SQL, therefore either one would work.so I guess perhaps you can construct a SQL as simple as what Pawan Kumar Khowal mentioned in ID: 41819090, but what you need to do is to build a dynamic query in your Access form with some macro scripts.
The list is something copied from a document, not saved in DB.
--
Select * from
(Select 'y' lst union all select 'i' lst union all SELECT 'for' lst union all SELECT 'xyz' lst union all SELECT 'abc' lst) a
EXCEPT
(select mytable.column1 from mytable) b
--
Select * from
(Select 'y' lst union all select 'i' lst union all SELECT 'for' lst union all SELECT 'xyz' lst union all SELECT 'abc' lst) a
WHERE a.ist NOT IN
(select mytable.column1 from mytable)
DECLARE @List AS VARCHAR(200) = '''y'',''i'',''for'',''xyz'',''abc'''
--select @List
;WITH
D1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
D2(N) AS (SELECT 1 FROM D1 a, D1 b),
D4(N) AS (SELECT 1 FROM D2 a, D2 b)
, Numbers AS (
SELECT TOP (1000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number FROM D4
)
, Missing AS (
SELECT
SUBSTRING(@List,N.Number+1,CHARINDEX(',',@List+',',N.Number+1)-N.Number-1) AS Data
FROM Numbers n
WHERE N.Number < LEN(@List)
AND SUBSTRING(@List,N.Number,1) = ','
)
SELECT replace(Missing.data,'''','') as Missing_values
FROM Missing
LEFT JOIN (
select 'y' as column1 union all
select 'xyz' as column1 union all
select 'i' as column1
) Mytable ON replace(Missing.data,'''','') = Mytable.Column1
WHERE Mytable.Column1 IS NULL
result:
Missing_values
-----
for
abc
(2 row(s) returned)
nb line 18 of the query @List+',' adding the trailing comma avoids a negative number and hence an error when using substringDECLARE @List AS VARCHAR(200) = 'y,i,for,xyz,abc'
--select @List
;WITH
D1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
D2(N) AS (SELECT 1 FROM D1 a, D1 b),
D4(N) AS (SELECT 1 FROM D2 a, D2 b)
, Numbers AS (
SELECT TOP (1000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number FROM D4
)
, Missing AS (
SELECT
SUBSTRING(@List,N.Number+1,CHARINDEX(',',@List+',',N.Number+1)-N.Number-1) AS Data
FROM Numbers n
WHERE N.Number < LEN(@List)
AND SUBSTRING(@List,N.Number,1) = ','
)
SELECT Missing.data as missing_values
FROM Missing
LEFT JOIN (
select 'y' as column1 union all
select 'xyz' as column1 union all
select 'i' as column1
) Mytable ON Missing.data = Mytable.Column1
WHERE Mytable.Column1 IS NULL
declare
@list varchar(8000)='y,i,for,xyz,abc'
select
@list_not_in_table=replace(@list+',',vals.column1+',','')
from
(
select distinct
column1
from
mytable
) vals
select @list_not_in_table as list_not_in_table
if object_id('tempdb..#mytable') is not null
drop table #mytable
create table #mytable (column1 varchar(50))
insert into #mytable select col from (values ('y'),('xyz'),('i'))v(col)
declare
@list varchar(8000)='y,i,for,xyz,abc'
declare
@list_not_in_table varchar(8000)=@list+','
select
@list_not_in_table=replace(@list_not_in_table,vals.column1,'')
from
(
select distinct
column1+',' as column1
from
#mytable
) vals
-- get rid of the last comma
select @list_not_in_table=left(@list_not_in_table,len(@list_not_in_table)-1)
-- results
select @list as list, @list_not_in_table as list_not_in_table
DECLARE @x AS XML=''
DECLARE @Param AS VARCHAR(100) = 'y,i,xyz'
SET @x = CAST('<A>'+ REPLACE(@Param,',','</A><A>')+ '</A>' AS XML)
SELECT * FROM
(
SELECT t.value('.', 'VARCHAR(10)') Value FROM @x.nodes('/A') AS x(t)
)k
OUTER APPLY
(
SELECT * From yourtable b
WHERE b.[columne] = k.value
)
WHERE k.Value IS NULL
1. Using EXCEPTSelect * from
(Select 'y' lst union all select 'i' lst union all SELECT 'for' lst union all SELECT 'xyz' lst union all SELECT 'abc' lst) a
EXCEPT
(select mytable.column1 from mytable) b
Select * from
(Select 'y' lst union all select 'i' lst union all SELECT 'for' lst union all SELECT 'xyz' lst union all SELECT 'abc' lst) a
WHERE a.ist NOT IN
(select mytable.column1 from mytable)
Select * from
(Select 'y' lst union all select 'i' lst union all SELECT 'for' lst union all SELECT 'xyz' lst union all SELECT 'abc' lst) a
LEFT JOIN
(select mytable.column1 from mytable) x ON x.column1 = a.lst
WHERE x.column1 IS NULL
if object_id('tempdb..#mytable') is not null
drop table #mytable
create table #mytable (column1 varchar(50))
insert into #mytable select col from (values ('y'),('xyz'),('i'))v(col)
declare
@list varchar(8000)='y,i,for,xyz,abc'
declare
@list_not_in_table varchar(8000)=@list+','
select
@list_not_in_table=replace(@list_not_in_table,vals.column1,'')
from
(
select distinct
column1+',' as column1
from
#mytable
) vals
-- get rid of the last comma
select @list_not_in_table=left(@list_not_in_table,len(@list_not_in_table)-1)
-- results
select @list as list, @list_not_in_table as list_not_in_table
(3 row(s) affected)
list list_not_in_table
------------------------- -------------------------
y,i,for,xyz,abc for,abc
;WITH
D1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
D2(N) AS (SELECT 1 FROM D1 a, D1 b),
D4(N) AS (SELECT 1 FROM D2 a, D2 b)
, Numbers AS (
SELECT TOP (1000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Number FROM D4
)
, Missing AS (
SELECT
SUBSTRING('y,i,for,xyz,abc',N.Number+1,CHARINDEX(',','y,i,for,xyz,abc'+',',N.Number+1)-N.Number-1) AS Data
FROM Numbers n
WHERE N.Number < LEN('y,i,for,xyz,abc')
AND SUBSTRING('y,i,for,xyz,abc',N.Number,1) = ','
)
SELECT Missing.data as Missing_values
FROM Missing
LEFT JOIN (
select 'y' as column1 union all
select 'xyz' as column1 union all
select 'i' as column1
) Mytable ON Missing.data = Mytable.Column1
WHERE Mytable.Column1 IS NULL
How are you executing the sql? Through what tool?I can use whatever method you suggest..currently using SSMS 2008 linked to a 2005 DB.
What is the relevance of SharePoint to this question?Looks like EE changed their site and some things get pre-entered in the topics area as I did not select that..
Actually my pc has version 2008 its just the server where the DB resides has 2005.That is the problem. 2005 doesn't have that syntax for VALUES with multiple rows. This will work:
if object_id('tempdb..#mytable') is not null
drop table #mytable
create table #mytable (column1 varchar(50))
insert into #mytable values ('y')
insert into #mytable values ('xyz')
insert into #mytable values ('i')
declare
@list varchar(8000)='y,i,for,xyz,abc'
declare
@list_not_in_table varchar(8000)=@list+','
select
@list_not_in_table=replace(@list_not_in_table,vals.column1,'')
from
(
select distinct
column1+',' as column1
from
#mytable
) vals
-- get rid of the last comma
select @list_not_in_table=left(@list_not_in_table,len(@list_not_in_table)-1)
-- results
select @list as list, @list_not_in_table as list_not_in_table
if object_id('tempdb..#mytable') is not null
drop table #mytable
create table #mytable (column1 varchar(50))
insert into #mytable values ('y')
insert into #mytable values ('xyz')
insert into #mytable values ('i')
declare
@list varchar(8000),
@list_not_in_table varchar(8000)
set @list='y,i,for,xyz,abc'
set @list_not_in_table=@list+','
select
@list_not_in_table=replace(@list_not_in_table,vals.column1,'')
from
(
select distinct
column1+',' as column1
from
#mytable
) vals
-- get rid of the last comma
select @list_not_in_table=left(@list_not_in_table,len(@list_not_in_table)-1)
-- results
select @list as list, @list_not_in_table as list_not_in_table
---
IF OBJECT_ID('TEMPDB..#MYTABLE') IS NOT NULL
DROP TABLE #MYTABLE
CREATE TABLE #MYTABLE (COLUMN1 VARCHAR(50))
INSERT INTO #MYTABLE VALUES ('Y')
INSERT INTO #MYTABLE VALUES ('XYZ')
INSERT INTO #MYTABLE VALUES ('I')
DECLARE @x AS XML=''
DECLARE @Param AS VARCHAR(100) = 'y,i,for,xyz,abc'
SET @x = CAST('<A>'+ REPLACE(@Param,',','</A><A>')+ '</A>' AS XML)
DECLARE @list_not_in_table AS VARCHAR(1000) = ''
SELECT @list_not_in_table = @list_not_in_table + Value + ',' FROM
(
SELECT t.value('.', 'VARCHAR(10)') Value FROM @x.nodes('/A') AS x(t)
)k
OUTER APPLY
(
SELECT * From #mytable b
WHERE b.column1 = k.value
)z
WHERE z.column1 IS NULL
---
---
IF OBJECT_ID('TEMPDB..#MYTABLE') IS NOT NULL
DROP TABLE #MYTABLE
CREATE TABLE #MYTABLE (COLUMN1 VARCHAR(50))
INSERT INTO #MYTABLE VALUES ('Y')
INSERT INTO #MYTABLE VALUES ('XYZ')
INSERT INTO #MYTABLE VALUES ('I')
DECLARE @x AS XML=''
DECLARE @Param AS VARCHAR(100) = 'y,i,for,xyz,abc'
SET @x = CAST('<A>'+ REPLACE(@Param,',','</A><A>')+ '</A>' AS XML)
DECLARE @list_not_in_table AS VARCHAR(1000) = ''
SELECT @list_not_in_table = @list_not_in_table + Value + ','
FROM
(
SELECT t.value('.', 'VARCHAR(10)') Value FROM @x.nodes('/A') AS x(t)
EXCEPT
SELECT COLUMN1 FROM #MYTABLE
)r
SELECT @Param RecordsIntheList , SUBSTRING(@list_not_in_table, 1, NULLIF(DATALENGTH(@list_not_in_table)-1,-1)) ListNotInTable
---
---
IF OBJECT_ID('TEMPDB..#MYTABLE') IS NOT NULL
DROP TABLE #MYTABLE
CREATE TABLE #MYTABLE (COLUMN1 VARCHAR(50))
INSERT INTO #MYTABLE VALUES ('Y')
INSERT INTO #MYTABLE VALUES ('XYZ')
INSERT INTO #MYTABLE VALUES ('I')
DECLARE @x AS XML=''
DECLARE @Param AS VARCHAR(100) = 'y,i,for,xyz,abc'
SET @x = CAST('<A>'+ REPLACE(@Param,',','</A><A>')+ '</A>' AS XML)
DECLARE @list_not_in_table AS VARCHAR(1000) = ''
SELECT @list_not_in_table = @list_not_in_table + Value + ','
FROM
(
SELECT * FROM
(
SELECT t.value('.', 'VARCHAR(10)') Value FROM @x.nodes('/A') AS x(t)
)x
WHERE Value NOT IN ( SELECT COLUMN1 FROM #MYTABLE )
)r
SELECT @Param RecordsIntheList , SUBSTRING(@list_not_in_table, 1, NULLIF(DATALENGTH(@list_not_in_table)-1,-1)) ListNotInTable
---
---
IF OBJECT_ID('TEMPDB..#MYTABLE') IS NOT NULL
DROP TABLE #MYTABLE
CREATE TABLE #MYTABLE (COLUMN1 VARCHAR(50))
INSERT INTO #MYTABLE VALUES ('Y')
INSERT INTO #MYTABLE VALUES ('XYZ')
INSERT INTO #MYTABLE VALUES ('I')
DECLARE @x AS XML=''
DECLARE @Param AS VARCHAR(100) = 'y,i,for,xyz,abc'
SET @x = CAST('<A>'+ REPLACE(@Param,',','</A><A>')+ '</A>' AS XML)
DECLARE @list_not_in_table AS VARCHAR(1000) = ''
SELECT @list_not_in_table = @list_not_in_table + Value + ','
FROM
(
SELECT * FROM
(
SELECT t.value('.', 'VARCHAR(10)') Value FROM @x.nodes('/A') AS x(t)
)x
LEFT JOIN ( SELECT COLUMN1 FROM #MYTABLE )t ON t.COLUMN1 = x.Value
WHERE t.COLUMN1 IS NULL
)r
SELECT @Param RecordsIntheList , SUBSTRING(@list_not_in_table, 1, NULLIF(DATALENGTH(@list_not_in_table)-1,-1)) ListNotInTable
---
Open in new window
Enjoy !!