Solved

Strange behaviour of sp_executesql with xml parameter

Posted on 2016-08-08
8
44 Views
Last Modified: 2016-10-09
I am getting various results from 'EXEC sp_executesql @sql, N'@xml xml', @xml', depending on how I construct the @sql variable. Only 2 of these constructs are successful. All others fail, with different error messages. Whether successful or not, the only thing that changes is the number of repetitions of a single field in the SELECT clause, and whether they are on a single line, or separated by carriage returns.

The general construct is:

   SET @sql='
   SELECT
   (repeated field)
   FROM @xml.nodes(''/row'') As t(c)
   INNER JOIN [SQLRONA].[dbo].[T_item] ON [SQLRONA].[dbo].[T_item].[CodeRona]=t.c.value(''@sk'',''char(8)'')'

First, here are the 2 field repetitions that work:

   1) [SQLRONA].[dbo].[T_item].[CodeRona] (N.B.: Not a repetition. Just a single field.)
   2) [SQLRONA].[dbo].[T_item].[CodeRona], [SQLRONA].[dbo].[T_item].[CodeRona]

Following are some of the failing repetitions, and their error messages:

   3) [SQLRONA].[dbo].[T_item].[CodeRona], [SQLRONA].[dbo].[T_item].[CodeRona], [SQLRONA].[dbo].[T_item].[CodeRona]
       Incorrect syntax near '('.

   4) [SQLRONA].[dbo].[T_item].[CodeRona],
       [SQLRONA].[dbo].[T_item].[CodeRona],
       [SQLRONA].[dbo].[T_item].[CodeRona]
       Unclosed quotation mark after the character string 'Code'. Incorrect syntax near 'Code'.

   5) [SQLRONA].[dbo].[T_item].[CodeRona], [SQLRONA].[dbo].[T_item].[CodeRona], [SQLRONA].[dbo].[T_item].[CodeRona], [SQLRONA].[dbo].[T_item].[CodeRona]
       An expression of non-boolean type specified in a context where a condition is expected, near 'SQLRONA'.

   6) [SQLRONA].[dbo].[T_item].[CodeRona],
       [SQLRONA].[dbo].[T_item].[CodeRona],
       [SQLRONA].[dbo].[T_item].[CodeRona],
       [SQLRONA].[dbo].[T_item].[CodeRona]
       Unclosed quotation mark after the character string 'db'. Incorrect syntax near 'db'.
       
Can anyone explain why sp_executesql is behaving like that?
0
Comment
Question by:J2015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41747472
The format for sp_executesql does not look correct.

By definition, it should be:
'EXEC sp_executesql @sql, N'@xml xml', @xml = <value>'

Where are you setting the passed in parameter equal to a value?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41747474
You can see examples at Microsoft's page for sp_executesql:
https://msdn.microsoft.com/en-us/library/ms188001.aspx
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41747477
Correction to the definition. There shouldn't have been an extra apostrophe at the end:
'EXEC sp_executesql @sql, N'@xml xml', @xml = <value>
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:J2015
ID: 41747523
Here's the entire code:

USE [SQLXRAY_FE_EXTENSION]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_FetchXml] @List varchar(Max)
AS
DECLARE @xml xml, @sql nvarchar(255)
BEGIN
      SET NOCOUNT ON
      SET @xml=CAST(@List As XML)
      SET @sql=' SELECT [SQLRONA].[dbo].[T_item].[CodeRona] FROM @xml.nodes(''/row'') As t(c)
      INNER JOIN [SQLRONA].[dbo].[T_item]
      ON [SQLRONA].[dbo].[T_item].[CodeRona]=t.c.value(''@sk'',''char(8)'')'
      EXEC sp_executesql @sql, N'@xml xml', @xml
END

I call the stored procedure with the following query:

usp_FetchXml '<row sk="7805059" /> <row sk="0001982" /> <row sk="11635000" /> <row sk="11635001" />'
0
 

Author Comment

by:J2015
ID: 41747552
I forgot to mention: I am using SQL Server 2008 R2.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41747680
This line:
EXEC sp_executesql @sql, N'@xml xml', @xml

should be:
EXEC sp_executesql @sql, N'@xml xml', @xml = @xml

To make it clearer:
EXEC sp_executesql @sql, N'@xmlData xml', @xmlData = @xml
0
 

Accepted Solution

by:
J2015 earned 0 total points
ID: 41747851
I found why it fails. The line DECLARE @xml xml, @sql nvarchar(255)
should be DECLARE @xml xml, @sql nvarchar(MAX).

Thanks.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

759 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