Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 101
  • Last Modified:

Strange behaviour of sp_executesql with xml parameter

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
J2015
Asked:
J2015
  • 4
  • 3
1 Solution
 
Shaun KlineLead Software EngineerCommented:
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
 
Shaun KlineLead Software EngineerCommented:
You can see examples at Microsoft's page for sp_executesql:
https://msdn.microsoft.com/en-us/library/ms188001.aspx
0
 
Shaun KlineLead Software EngineerCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
J2015Author Commented:
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
 
J2015Author Commented:
I forgot to mention: I am using SQL Server 2008 R2.
0
 
Shaun KlineLead Software EngineerCommented:
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
 
J2015Author Commented:
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now