|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
sp_executesql: How to have a parameter within a stringexec 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. Johnny Ortega wrote:
Show quote > If I execute You'll have to use dynamic SQL to do this... Build the command as a > 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. > > string, and then execute the string using EXEC Tracy McKibben (tr***@realsqlguy.com) writes:
> Johnny Ortega wrote: Looks to me that Johnny is already using dynamic SQL....>> 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 -- 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 Johnny Ortega (john.ort***@quest.com) writes:
Show quote > If I execute Apparently you must specify EXEC when you have a parameter. Keep in> 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? 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 |
|||||||||||||||||||||||