Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying XML data

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
US
I'm using SQL Server 2008 R2.

I have a table with a xml data type column. I'm trying to query one of the elements from the column's value, but getting the following error:

Msg 2389, Level 16, State 1, Line 12
XQuery [tblEmailFollowup.OptionalParameters.value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

SQL:
CREATE TABLE tblEmailFollowup
(
	EmailFollowupID int IDENTITY(1,1)
	,OptionalParameters xml
)

DECLARE @Parameter XML
SET @Parameter = '<Parameter><UserType>DonorManager</UserType></Parameter>'

INSERT INTO tblEmailFollowup(OptionalParameters)Values(@Parameter)

SELECT ref.value('UserType', 'nvarchar(50)') AS [UserType]
FROM tblEmailFollowup
CROSS APPLY OptionalParameters.nodes('//Parameter') R(ref)

What am I doing wrong?

Thanks for the help.
 
Code:
DECLARE @tblEmailFollowup TABLE 
(
	EmailFollowupID int IDENTITY(1,1)
	,OptionalParameters xml
)

DECLARE @Parameter XML
SET @Parameter = '<Parameter><UserType>DonorManager</UserType></Parameter>'

INSERT INTO @tblEmailFollowup(OptionalParameters) Values (@Parameter)

SET @Parameter = '<Parameter><UserType>DonorManager1</UserType></Parameter>'
INSERT INTO @tblEmailFollowup(OptionalParameters) Values (@Parameter)

SELECT r.ref.value('(/Parameter/UserType)[1]','varchar(50)')
FROM @tblEmailFollowup
CROSS APPLY  OptionalParameters.nodes('/Parameter') R(ref)

Borislav Borissov
VFP9 SP2, SQL Server
 
Thanks Boris.

What does the [1] do?

Also, do you recommend to use nvarchar only when it's required because of the extra memory it takes?
 

BTW your original query also works if you place [1] :)
Code:
CREATE TABLE tblEmailFollowup
(
	EmailFollowupID int IDENTITY(1,1)
	,OptionalParameters xml
)

DECLARE @Parameter XML
SET @Parameter = '<Parameter><UserType>DonorManager</UserType></Parameter>'

INSERT INTO tblEmailFollowup(OptionalParameters)Values(@Parameter)

SELECT ref.value('UserType[1]', 'nvarchar(50)') AS [UserType]
FROM tblEmailFollowup
CROSS APPLY OptionalParameters.nodes('//Parameter') R(ref)

Borislav Borissov
VFP9 SP2, SQL Server
 
In your example

SQL:
DECLARE @tblEmailFollowup TABLE 
(
	EmailFollowupID int IDENTITY(1,1)
	,OptionalParameters xml
)

DECLARE @Parameter XML
SET @Parameter = '<Parameter><UserType>DonorManager</UserType></Parameter>'

INSERT INTO @tblEmailFollowup(OptionalParameters) Values (@Parameter)

SET @Parameter = '<Parameter><UserType>DonorManager1</UserType></Parameter>'
INSERT INTO @tblEmailFollowup(OptionalParameters) Values (@Parameter)

SELECT r.ref.value('(/Parameter/UserType)[1]','varchar(50)')
FROM @tblEmailFollowup
CROSS APPLY  OptionalParameters.nodes('/Parameter') R(ref)

What would be the easiest way to get the EmailFollowupID where OptionalParameters = DonorManager1
 
I used this:

SQL:
SELECT  EmailFollowupID
FROM tblEmailFollowup
CROSS APPLY OptionalParameters.nodes('//Parameter') R(ref) 
WHERE ref.value('UserType[1]', 'nvarchar(50)') = 'DonorManager'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top