Home All Groups Group Topic Archive Search About

sp_executesql: How to have a parameter within a string

Author
13 Jul 2006 4:50 PM
Johnny Ortega
If I execute
exec sp_executesql N'sp_helpindex N''[dbo].[authors]'''

within EM/Mgmt Studio, this statement runs fine. If I try and parameterize
it, like this:
exec sp_executesql N'sp_helpindex N''@QualifiedTableName''',
N'@QualifiedTableName nvarchar(15)', @QualifiedTableName =
N'[dbo].[authors]'

I get
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'sp_helpindex'.

I tried different variations, but nothing seemed to work. Is it not possible
to have a parameter within a string like this?

Thanks,

Johnny.

Author
13 Jul 2006 5:04 PM
Tracy McKibben
Johnny Ortega wrote:
Show quote
> If I execute
> exec sp_executesql N'sp_helpindex N''[dbo].[authors]'''
>
> within EM/Mgmt Studio, this statement runs fine. If I try and parameterize
> it, like this:
> exec sp_executesql N'sp_helpindex N''@QualifiedTableName''',
> N'@QualifiedTableName nvarchar(15)', @QualifiedTableName =
> N'[dbo].[authors]'
>
> I get
> Server: Msg 170, Level 15, State 1, Line 1
> Line 1: Incorrect syntax near 'sp_helpindex'.
>
> I tried different variations, but nothing seemed to work. Is it not possible
> to have a parameter within a string like this?
>
> Thanks,
>
> Johnny.
>
>

You'll have to use dynamic SQL to do this...  Build the command as a
string, and then execute the string using EXEC


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
13 Jul 2006 10:22 PM
Erland Sommarskog
Tracy McKibben (tr***@realsqlguy.com) writes:
> Johnny Ortega wrote:
>> within EM/Mgmt Studio, this statement runs fine. If I try and
>> parameterize
>> it, like this:
>> exec sp_executesql N'sp_helpindex N''@QualifiedTableName''',
>> N'@QualifiedTableName nvarchar(15)', @QualifiedTableName =
>> N'[dbo].[authors]'
>
> You'll have to use dynamic SQL to do this...  Build the command as a
> string, and then execute the string using EXEC

Looks to me that Johnny is already using dynamic SQL....


--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Author
13 Jul 2006 10:22 PM
Erland Sommarskog
Johnny Ortega (john.ort***@quest.com) writes:
Show quote
> If I execute
> exec sp_executesql N'sp_helpindex N''[dbo].[authors]'''
>
> within EM/Mgmt Studio, this statement runs fine. If I try and parameterize
> it, like this:
> exec sp_executesql N'sp_helpindex N''@QualifiedTableName''',
> N'@QualifiedTableName nvarchar(15)', @QualifiedTableName =
> N'[dbo].[authors]'
>
> I get
> Server: Msg 170, Level 15, State 1, Line 1
> Line 1: Incorrect syntax near 'sp_helpindex'.
>
> I tried different variations, but nothing seemed to work. Is it not
> possible to have a parameter within a string like this?

Apparently you must specify EXEC when you have a parameter. Keep in
mind that in

   sp_helpindex N'[dbo].[authors]'

There is an implicit EXEC when the first token of a batch is an identifier.
This fails:

   sp_helpindex N'[dbo].[authors]'
   sp_helpindex N'[dbo].[titles]'

Here you must specify EXEC for the second call to sp_helpindex.

Really why you have to specify EXEC when you add a parameter, I don't
know. I am a little puzzled myself. But since best practice is to always
include EXEC, I'm not going to lose sleep over it.

Once you have added EXEC, you will run into the next snag, but I leave
it do you as an exercise to find out what the problem is.

....and a final remark, is that it is much simpler to say:

   EXEC sp_helpindex @QualifiedTableName

little point in using dynamic SQL.

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

AddThis Social Bookmark Button