I will attempt to explain this situation as clearly as I can. I have this query below which tries to get a list of author_ids from an xml column in sql server 2005 associated with a book, but assigning the query to the @authorIds returns an error because it returns multiple rows, any ideas on how to run around this problem? Many thanks guys.
DECLARE @BookAuthor TABLE (ID int)
DECLARE @authorIds xml
SET @authorIds = (select ListOfAuthors.query('/Authors/value') from BookRelated where ListOfAuthors is not null)
INSERT INTO @BookAuthor (ID) SELECT ParamValues.ID.value('.','VARCHAR(20)')
FROM @authorIds.nodes('/Authors/value') as ParamValues(ID)
SELECT a.ID,
FullName AS Author
FROM Author auth
INNER JOIN @BookAuthor a
ON a.ID = auth.Id
you have a bug on the line "FROM @authorIds.nodes('/Authors/value') as ParamValues(ID)" it should be "FROM @authorIds.nodes('value') as ParamValues(ID)"
--- Test Data
DECLARE @BookRelated TABLE (ListOfAuthors XML)
DECLARE @Author TABLE (ID int, FullName VARCHAR(100))
INSERT INTO @BookRelated (ListOfAuthors) VALUES ('<Authors><value>1</value></Authors>')
INSERT INTO @Author (ID, FullName) VALUES (1, 'Matt')
-- test script
DECLARE @BookAuthor TABLE (ID int)
DECLARE @authorIds xml
SET @authorIds = (
select ListOfAuthors.query('/Authors/value')
from @BookRelated
where ListOfAuthors is not null)
INSERT INTO @BookAuthor (ID)
SELECT ParamValues.ID.value('.','VARCHAR(20)')
FROM @authorIds.nodes('value') as ParamValues(ID)
SELECT a.ID, FullName AS Author
FROM @Author auth
INNER JOIN @BookAuthor a ON a.ID = auth.Id