Showing posts with label real. Show all posts
Showing posts with label real. Show all posts

Friday, March 9, 2012

Join XML fragment with real table

I was wondering how someone would go about joining an XML fragment with a SQL table. Say for example this is my XML fragment.

<foo><key>1</key><description>One</description></foo>

<foo><key>2</key><description>Two</description></foo>

My table looks like this:

key SomeInfo

1 SomeInfo1

2 SomeInfo2

I want to be able to create a Stored procedure that takes my XML fragment as an xml type and then create a SQL select statement that looks like this in pseudo code.

Select myTable.key, myTable.SomeInfo, @.myXML.Description

From myTable and @.myXML

Where myTable.key = @.myXML.key

The result would then look like this

key SomeInfo Description

1 SomeInfo1 One

2 SomeInfo2 Two

Anyone have any ideas. I've got it where my XML and my table are joined by doing an @.myXML.exist(/foo/key[.=sql:column("myTable.key")] = 1 thing but I can't get the right Description value to show up in my row. I always get the first row.

Just to be complete. Here's my present stab at it.

Declare @.myXML xml

Set @.myXML = '<foo><key>1</key><description>One</description></foo>

<foo><key>2</key><description>Two</description></foo>'

Select myTable.key, myTable.SomeInfo, @.myTable.value('/foo/Description[1]','varchar(15)')

From myTable

Where @.myXML.exist('/foo/key[.=sql:column("myTable.key")]') = 1

I think you are looking for the XQuery "nodes()" function. It sounds like you want to reach into the column that is storing the XML, extract scalar values from that xml and join it with a relational table. If this is what you are trying to do, then the nodes() function should work for you.

Here is an example using the nodes() function on the xml data type.

http://msdn2.microsoft.com/en-us/ms188282.aspx

If you go this route, then your query would be something like:

SELECT myTable.key, myTable.SomeInfo, T(c).value('/description[1]', 'varchar(max)') as Description
FROM myTable, @.myXml.nodes('/foo') AS T(c)
WHERE myTable.key = T(c).value('/key[1]', 'int')

|||

Thanks for the reply,

I tried what you suggested and I actually don't get any results back at all. I first had to change the T(c).value to be T.c.value to get it to work and then when it ran it gave me back an empty result set.

|||

Sorry about that I pressed enter before putting in my final results.

Your suggestion worked very well. Here is the final working query:

Select myTable.key,myTable.SomeInfo, T.c.value('./description[1]','varchar(max)') As Description

from myTable,@.myXML.nodes('/foo') As T(c)

Where myTable.key = T.c.value('./key[1]','int')

Friday, February 24, 2012

Join Returns too many rows

Hi

I'm sure this is a real noob question and it may be something I have know the answer to in the past but I can't remember and its been driving me mad for hours. If anyone can tell me how to do this it would make my day!

I have simplified the problem for the purpose of clarity and have attached a sript to create a simple example table.

the table looks like this:

id cMatch cData
1 A A1
2 B B1
3 C C1
4 B B2
5 A A2
6 B B3

I want to be able to do a join on the two table that only returns the following:

t1.cData t2.cData
A1 A2
B1 B2
B1 B3

The Closest I can get is with the following qry:

SELECT t1.cdata, t2.cdata from tmp_Table1 t1
JOIN tmp_Table1 t2 ON t1.cMatch=t2.cMatch AND t1.cdata<>t2.cdata
WHERE t1.cdata<t2.cdata
ORDER BY t1.cdata, t2.cdata

Which returns:

t1.cData t2.cData
A1 A2
B1 B2
B1 B3
B2 B3Not sure if I attached the script last time so I thought I'd make sure.

thanks in advance for all your help!

Andy|||Hi

while not knowing your specific database, this will work with the example you provided:

SELECT MIN(cdata1), cdata2 FROM
(
SELECT t1.cdata AS cdata1, t2.cdata AS cdata2 FROM tmp_Table1 t1
INNER JOIN tmp_Table1 t2 ON t1.cMatch=t2.cMatch
AND t1.cData <> t2.cData
AND t1.id < t2.id)
AS subtable
GROUP BY cdata2

you may have to change the aggragation function that evaluates the correct value to choose.

JOIN Question

I'm not a real wizard with SQL but am working with it more lately. I've only
recently experimented with JOINS, and am having a problem with the following
.
It says there's an issue "near the JOIN".
Select HoldXref.HoldXrefId, HoldXref.FolderName,
HoldXref.ImageRecId,HoldXref.HoldListId, HoldList.HoldId,
HoldList.HoldType,HoldList.Comment, HoldList.UserName,
HoldTypes.HoldTypeId,HoldTypes.HoldName, HoldTypes.HoldDescription,
HoldTypes.DateBased From HoldXref Where HoldXref.FolderName='Dept_06' And
ImageRecId = 11 INNER JOIN HoldList On HoldXref.HoldListId = HoldList.HoldId
INNER JOIN HoldTypes On HoldList.HoldType = HoldTypes.HoldTypeId
This seems really complicated. When I experimented with it originally, I
didn't have the "where HoldXref.FolderName='Dept_06' and ImageRecId = 11"
It was just a prototype and it worked. However, I want to limit the output
to this criteria and am not sure how to do that. Maybe it's as simple as
putting parenthesis around them, but I've tried several things and it's not
working. And for one reason or another, the stuff I find in the book(s) don'
t
happen to have an example similar to what I'm trying to do.You have your WHERE clause embedded before your JOINs... Please work on your
formatting a bit. It will help you resolve problems like these much more
easily.
SELECT
HoldXref.HoldXrefId,
HoldXref.FolderName,
HoldXref.ImageRecId,
HoldXref.HoldListId,
HoldList.HoldId,
HoldList.HoldType,
HoldList.Comment,
HoldList.UserName,
HoldTypes.HoldTypeId,
HoldTypes.HoldName,
HoldTypes.HoldDescription,
HoldTypes.DateBased
FROM HoldXref
INNER JOIN HoldList On HoldXref.HoldListId = HoldList.HoldId
INNER JOIN HoldTypes On HoldList.HoldType = HoldTypes.HoldTypeId
WHERE
HoldXref.FolderName='Dept_06'
And ImageRecId = 11
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"Les Stockton" <LesStockton@.discussions.microsoft.com> wrote in message
news:96A94732-929E-4756-BB3E-E60ABC720D56@.microsoft.com...
> I'm not a real wizard with SQL but am working with it more lately. I've
only
> recently experimented with JOINS, and am having a problem with the
following.
> It says there's an issue "near the JOIN".
> Select HoldXref.HoldXrefId, HoldXref.FolderName,
> HoldXref.ImageRecId,HoldXref.HoldListId, HoldList.HoldId,
> HoldList.HoldType,HoldList.Comment, HoldList.UserName,
> HoldTypes.HoldTypeId,HoldTypes.HoldName, HoldTypes.HoldDescription,
> HoldTypes.DateBased From HoldXref Where HoldXref.FolderName='Dept_06' And
> ImageRecId = 11 INNER JOIN HoldList On HoldXref.HoldListId =
HoldList.HoldId
> INNER JOIN HoldTypes On HoldList.HoldType = HoldTypes.HoldTypeId
>
> This seems really complicated. When I experimented with it originally, I
> didn't have the "where HoldXref.FolderName='Dept_06' and ImageRecId = 11"
> It was just a prototype and it worked. However, I want to limit the output
> to this criteria and am not sure how to do that. Maybe it's as simple as
> putting parenthesis around them, but I've tried several things and it's
not
> working. And for one reason or another, the stuff I find in the book(s)
don't
> happen to have an example similar to what I'm trying to do.|||>> It says there's an issue "near the JOIN".
Why do you have the INNER JOIN clause after the WHERE clause? Did you check
the exact syntax for a SELECT statement in SQL Server Books Online and
looked at the examples given there?
Anith