我会尽可能清楚地解释这种情况。我在下面有这个查询,试图从sql server 2005中的一个xml列中获取一个author_ids列表,但是将该查询分配给@authorIds会返回一个错误,因为它会返回多行,有关如何运行的任何想法这个问题?非常感谢你们。
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
答案 0 :(得分:1)
您在“FROM @ authorIds.nodes('/ Authors / value')”行中有一个错误,因为ParamValues(ID)“它应该是”FROM @ authorIds.nodes('value')为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