Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trying to parameterise code executed by sp_executesql


Trying to parameterise code executed by sp_executesql

Author
Message
Scott In Sydney
Scott In Sydney
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 172
I'm trying to parameterise code executed by sp_executesql. It's not a stored procedure, I've just got a number of similar SQL scripts, and trying to minimise the edits involved.

Here is some sample code (simplified example):

declare @tablename nvarchar(255);
declare @colname nvarchar(255);
declare @parms nvarchar(255);
declare @sql nvarchar(max);

set @tablename=N'sys.objects';
set @colname=N'name';
set @parms=N'@tablename nvarchar(255)';
set @sql=N'select top 5 * from @tablename';
exec sp_executesql @sql, @parms, @tablename;
------------------------------------------------
declare @tablename nvarchar(255);
declare @colname nvarchar(255);
declare @parms nvarchar(255);
declare @sql nvarchar(max);

set @tablename=N'sys.objects';
set @colname=N'name';
set @parms=N'@colname nvarchar(255)';
set @sql=N'select top 5 @colname from sys.objects';
exec sp_executesql @sql, @parms, @colname;
------------------------------------------------
declare @tablename nvarchar(255);
declare @colname nvarchar(255);
declare @parms nvarchar(255);
declare @sql nvarchar(max);

set @tablename=N'sys.objects';
set @colname=N'name';
set @parms=N'@tablename nvarchar(255), @colname nvarchar(255)';
set @sql=N'select top 5 @colname from @tablename';
exec sp_executesql @sql, @parms, @tablename, @colname;



Highlight each separate section and submit in SSMS.

I'm following the examples in here: http://msdn.microsoft.com/en-au/library/ms188001.aspx.

If I have to generate code like:

set @sql=N'select top 5 ' + @colname + ' from ' + @tablename;



Then I may consider this approach instead, especially when double- and triple-quoting is involved:

set @sql=N'select top 5 <<colname>> from <<tablename>>';
set @sql=replace(@sql,'<<colname>>',@colname);
set @sql=replace(@sql,'<<tablename>>',@tablename);
exec(@sql);



Thank you for pointing me in the right direction.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47241 Visits: 44377
You can't parameterise table or column names. You'll have to build up the string with those values in (and beware SQL injection vulnerabilities)


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17586 Visits: 32265
You parameterize the values passed to the query, not the tables and columns. Those have to be explicitly stated. As Gail said, you can build those statements dynamically, but you better have great syntax checking to ensure you don't hit SQL Injection.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search