Solved

Join requires all to exist

Posted on 2008-10-10
5
160 Views
Last Modified: 2013-11-05
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"/><SourceItem ItemValue="S1IV2" SequenceNo="2"/><SourceItem 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.

0
Comment
Question by:gnidde1967
  • 3
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22685336
use LEFT JOIN instead of JOIN
0
 

Author Comment

by:gnidde1967
ID: 22685384
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].[MappingSourceItem]') 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"/><SourceItem ItemValue="S1IV2" SequenceNo="2"/><SourceItem 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

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22685455
ok, I see:
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 ( MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV1',  1);

INSERT INTO MappingSourceItem ( MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV2',  2);

INSERT INTO MappingSourceItem ( MappingSourceID, ItemValue, SequenceNo ) VALUES (43 , 'S1IV3',  3);

INSERT INTO MappingSourceItem ( MappingSourceID, ItemValue, SequenceNo ) VALUES (44 , 'S1IV1',  1);

INSERT INTO MappingSourceItem ( MappingSourceID, ItemValue, SequenceNo ) VALUES (44 , 'S1IV2',  2);
 

DECLARE @Source varchar(8000)

SET @Source = '<ROOT>

<SourceItem ItemValue="S1IV1" SequenceNo="1"/>

<SourceItem ItemValue="S1IV2" SequenceNo="2"/>

<SourceItem ItemValue="S1IV3" SequenceNo="3"/>

</ROOT>'
 

DECLARE @DocHandleSource int
 

EXEC sp_xml_preparedocument @DocHandleSource OUTPUT, @Source      
 

declare @mid int

set @mid = 43
 

;WITH s as (

  SELECT * FROM OPENXML (@DocHandleSource, '/ROOT/SourceItem',1)

   WITH (ItemValue  varchar(200), SequenceNo int) AS s

)

SELECT * 

  FROM s 

  JOIN MappingSourceItem AS msi

    ON msi.ItemValue = s.ItemValue 

   AND msi.SequenceNo = s.SequenceNo

   AND msi.MappingSourceID = @mid 

  WHERE NOT EXISTS ( select null 

                       from s 

                       left JOIN MappingSourceItem AS msi

                         ON msi.ItemValue = s.ItemValue 

                        AND msi.SequenceNo = s.SequenceNo

                        AND msi.MappingSourceID = @mid 

                       where msi.MappingSourceID is null

                     )

go

DROP TABLE [dbo].[MappingSourceItem]

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22685456
change:
set @mid = 43
to this:
set @mid = 44

to test with indeed 0 rows returned.
0
 

Author Comment

by:gnidde1967
ID: 22685645
Thanks angelIII,

Nice solution!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query question 4 37
How to find the last word in a string in SQLITE 14 69
How to query date ranges with SQL 6 37
SQL Query Syntax Join 4 27
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now