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?
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?