![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I'm using a 3rd-party app's back end which stores SQL statements in a table, so I have no choice but to use dynamic SQL to call them (unless someone else knows a workaround...) Problem is, I can't get the statement to run properly, and I can't see why. If I execute even a hard-coded variation like DECLARE @sql nvarchar(MAX) SET @sql ='SELECT foo FROM foostable' sp_executesql @sql I get: Incorrect syntax near 'sp_executesql'. If I run sp_executesql 'SELECT foo FROM foostable' I get: Procedure expects parameter '@statement' of type 'ntext/nchar/ nvarchar'. which I understand, as it's omitting the N converter--so if I run sp_executesql N'SELECT foo FROM foostable' it's fine. I don't understand why the first version fails. Is it some sort of implicit conversion downgrading @sql? Every variation of CAST and CONVERT I use has no effect. This is SQL Server 2005 SP2. Thanks in advance. |
#3
| |||
| |||
|
|
On May 1, 5:04 pm, downwitch <downwi... (AT) gmail (DOT) com> wrote: Hi, I'm using a 3rd-party app's back end which stores SQL statements in a table, so I have no choice but to use dynamic SQL to call them (unless someone else knows a workaround...) Problem is, I can't get the statement to run properly, and I can't see why. If I execute even a hard-coded variation like DECLARE @sql nvarchar(MAX) SET @sql ='SELECT foo FROM foostable' sp_executesql @sql I get: Incorrect syntax near 'sp_executesql'. If I run sp_executesql 'SELECT foo FROM foostable' I get: Procedure expects parameter '@statement' of type 'ntext/nchar/ nvarchar'. which I understand, as it's omitting the N converter--so if I run sp_executesql N'SELECT foo FROM foostable' it's fine. I don't understand why the first version fails. Is it some sort of implicit conversion downgrading @sql? Every variation of CAST and CONVERT I use has no effect. This is SQL Server 2005 SP2. Thanks in advance. Try printing your @sql parameter and then firing it mannually, you might find that the string is not what you expect. Anyway, that is my standard way of debugging dynamic sql.- Hide quoted text - - Show quoted text - |
#4
| |||
| |||
|
|
On May 1, 5:08 pm, manstein <jkelly.ad... (AT) gmail (DOT) com> wrote: On May 1, 5:04 pm, downwitch <downwi... (AT) gmail (DOT) com> wrote: Hi, I'm using a 3rd-party app's back end which stores SQL statements in a table, so I have no choice but to use dynamic SQL to call them (unless someone else knows a workaround...) Problem is, I can't get the statement to run properly, and I can't see why. If I execute even a hard-coded variation like DECLARE @sql nvarchar(MAX) SET @sql ='SELECT foo FROM foostable' sp_executesql @sql I get: Incorrect syntax near 'sp_executesql'. If I run sp_executesql 'SELECT foo FROM foostable' I get: Procedure expects parameter '@statement' of type 'ntext/nchar/ nvarchar'. which I understand, as it's omitting the N converter--so if I run sp_executesql N'SELECT foo FROM foostable' it's fine. I don't understand why the first version fails. Is it some sort of implicit conversion downgrading @sql? Every variation of CAST and CONVERT I use has no effect. This is SQL Server 2005 SP2. Thanks in advance. Try printing your @sql parameter and then firing it mannually, you might find that the string is not what you expect. Anyway, that is my standard way of debugging dynamic sql.- Hide quoted text - - Show quoted text - as an addendum, what is your (MAX) size? If its too small to hold all the characters in your string, your statement will be truncated and raise an error. |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
You are missing EXEC... It is optional only when you execute stored procedures that are the first statement in the batch. Just try: DECLARE @sql nvarchar(MAX) SET @sql ='SELECT foo FROM foostable' EXEC sp_executesql @sql HTH, Plamen Ratchevhttp://www.SQLStudio.com |
#7
| |||
| |||
|
|
Here's my version of the function: ----------- CREATE FUNCTION uQuoteString(@str nvarchar(MAX)) RETURNS nvarchar(MAX) AS BEGIN DECLARE @ret nvarchar(MAX), @sq nvarchar(4) SELECT @sq = '''' SELECT @ret = replace(@str, @sq, @sq + @sq) RETURN(@sq + @ret + @sq) END ----------- So running DECLARE @sql nvarchar(MAX) SET @sql ='SELECT foo FROM foostable' SET @sql = dbo.uQuoteString(@sql) EXEC sp_executesql @sql I now get: Incorrect syntax near 'SELECT foo FROM foostable' Note that the error has changed, no longer referencing the stored proc but instead the @sql argument. |
#8
| |||
| |||
|
|
as an addendum, what is your (MAX) size? |
#9
| |||
| |||
|
#10
| |||
| |||
|
|
manstein(jkelly.ad... (AT) gmail (DOT) com) writes: as an addendum, what is your (MAX) size? MAX implies in SQL 2005 a size of two gigabytes. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
![]() |
| Thread Tools | |
| Display Modes | |
| |