gnidde1967
asked on
Join requires all to exist
I have a tricky JOIN problem.
My stored procedure takes a xml in-parameter in which all rows together forms an unique identity. The number of rows is dynamic, e.g., there could be one row, two rows or even three rows that makes up the identity.
Example xml:
<ROOT>
<SourceItem ItemValue="S1IV1" SequenceNo="1"/>
<SourceItem ItemValue="S1IV2" SequenceNo="2"/>
<SourceItem ItemValue="S1IV3" SequenceNo="3"/>
</ROOT>
The corresponding table defining the identity looks as this:
CREATE TABLE [dbo].[MappingSourceItem](
[MappingSourceItemID] [int] IDENTITY(1,1) NOT NULL,
[MappingSourceID] [int] NOT NULL,
[ItemValue] [varchar](200) NOT NULL,
[SequenceNo] [int] NOT NULL
) ON [PRIMARY]
with the content
MappingSourceItemID MappingSourceID ItemValue SequenceNo
106; 43; S1IV1; 1
107; 43; S1IV2; 2
108; 43; S1IV3; 3
109; 44; S1IV1; 1
110; 44; S1IV2; 2
I then want to find out if the identity given in the example xml exists in the database.
As you can see it does for MappingSourceID=43 since all three rows in the xml mathes.
For MappingSourceID=44 it does not match since only the two first match. How do I write such a join where all rows must match?
My attempt below does not work since it returns all rows matching and does not care that all of them dont match.
DECLARE @Source varchar(MAX)
SET @Source = '<ROOT><SourceItem ItemValue="S1IV1" SequenceNo="1"/><SourceIte m ItemValue="S1IV2" SequenceNo="2"/><SourceIte m ItemValue="S1IV3" SequenceNo="3"/></ROOT>'
DECLARE @DocHandleSource int
EXEC sp_xml_preparedocument @DocHandleSource OUTPUT, @Source
SELECT * FROM MappingSourceItem AS msi
JOIN OPENXML (@DocHandleSource, '/ROOT/SourceItem',1)
WITH (ItemValue varchar(200), SequenceNo int) AS s
ON msi.ItemValue = s.ItemValue AND msi.SequenceNo = s.SequenceNo
WHERE msi.MappingSourceID = 44
will return the two rows for MappingSourceID=44 but I want it to return 0 rows since not all match.
My stored procedure takes a xml in-parameter in which all rows together forms an unique identity. The number of rows is dynamic, e.g., there could be one row, two rows or even three rows that makes up the identity.
Example xml:
<ROOT>
<SourceItem ItemValue="S1IV1" SequenceNo="1"/>
<SourceItem ItemValue="S1IV2" SequenceNo="2"/>
<SourceItem ItemValue="S1IV3" SequenceNo="3"/>
</ROOT>
The corresponding table defining the identity looks as this:
CREATE TABLE [dbo].[MappingSourceItem](
[MappingSourceItemID] [int] IDENTITY(1,1) NOT NULL,
[MappingSourceID] [int] NOT NULL,
[ItemValue] [varchar](200) NOT NULL,
[SequenceNo] [int] NOT NULL
) ON [PRIMARY]
with the content
MappingSourceItemID MappingSourceID ItemValue SequenceNo
106; 43; S1IV1; 1
107; 43; S1IV2; 2
108; 43; S1IV3; 3
109; 44; S1IV1; 1
110; 44; S1IV2; 2
I then want to find out if the identity given in the example xml exists in the database.
As you can see it does for MappingSourceID=43 since all three rows in the xml mathes.
For MappingSourceID=44 it does not match since only the two first match. How do I write such a join where all rows must match?
My attempt below does not work since it returns all rows matching and does not care that all of them dont match.
DECLARE @Source varchar(MAX)
SET @Source = '<ROOT><SourceItem ItemValue="S1IV1" SequenceNo="1"/><SourceIte
DECLARE @DocHandleSource int
EXEC sp_xml_preparedocument @DocHandleSource OUTPUT, @Source
SELECT * FROM MappingSourceItem AS msi
JOIN OPENXML (@DocHandleSource, '/ROOT/SourceItem',1)
WITH (ItemValue varchar(200), SequenceNo int) AS s
ON msi.ItemValue = s.ItemValue AND msi.SequenceNo = s.SequenceNo
WHERE msi.MappingSourceID = 44
will return the two rows for MappingSourceID=44 but I want it to return 0 rows since not all match.
use LEFT JOIN instead of JOIN
ASKER
No, that doesn't work.
Run the following script and you see that the two rows for MappingSourceID=44 will be returned and that is not desired. I want 0 rows to be returned.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Mapping SourceItem ]') AND type in (N'U'))
DROP TABLE [dbo].[MappingSourceItem]
GO
CREATE TABLE [dbo].[MappingSourceItem](
[MappingSourceItemID] [int] IDENTITY(1,1) NOT NULL,
[MappingSourceID] [int] NOT NULL,
[ItemValue] [varchar](200) NOT NULL,
[SequenceNo] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV1', 1);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV2', 2);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV3', 3);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (44 , 'S1IV1', 1);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (44 , 'S1IV2', 2);
DECLARE @Source varchar(MAX)
SET @Source = '<ROOT><SourceItem ItemValue="S1IV1" SequenceNo="1"/><SourceIte m ItemValue="S1IV2" SequenceNo="2"/><SourceIte m ItemValue="S1IV3" SequenceNo="3"/></ROOT>'
DECLARE @DocHandleSource int
EXEC sp_xml_preparedocument @DocHandleSource OUTPUT, @Source
SELECT * FROM MappingSourceItem AS msi
LEFT JOIN OPENXML (@DocHandleSource, '/ROOT/SourceItem',1)
WITH (ItemValue varchar(200), SequenceNo int) AS s
ON msi.ItemValue = s.ItemValue AND msi.SequenceNo = s.SequenceNo
WHERE msi.MappingSourceID = 44
Run the following script and you see that the two rows for MappingSourceID=44 will be returned and that is not desired. I want 0 rows to be returned.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Mapping
DROP TABLE [dbo].[MappingSourceItem]
GO
CREATE TABLE [dbo].[MappingSourceItem](
[MappingSourceItemID] [int] IDENTITY(1,1) NOT NULL,
[MappingSourceID] [int] NOT NULL,
[ItemValue] [varchar](200) NOT NULL,
[SequenceNo] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV1', 1);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV2', 2);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV3', 3);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (44 , 'S1IV1', 1);
INSERT INTO MappingSourceItem ( MappingSourceItemID, MappingSourceID, ItemValue, SequenceNo ) VALUES (44 , 'S1IV2', 2);
DECLARE @Source varchar(MAX)
SET @Source = '<ROOT><SourceItem ItemValue="S1IV1" SequenceNo="1"/><SourceIte
DECLARE @DocHandleSource int
EXEC sp_xml_preparedocument @DocHandleSource OUTPUT, @Source
SELECT * FROM MappingSourceItem AS msi
LEFT JOIN OPENXML (@DocHandleSource, '/ROOT/SourceItem',1)
WITH (ItemValue varchar(200), SequenceNo int) AS s
ON msi.ItemValue = s.ItemValue AND msi.SequenceNo = s.SequenceNo
WHERE msi.MappingSourceID = 44
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
change:
set @mid = 43
to this:
set @mid = 44
to test with indeed 0 rows returned.
set @mid = 43
to this:
set @mid = 44
to test with indeed 0 rows returned.
ASKER
Thanks angelIII,
Nice solution!
Nice solution!