August 29, 2010 at 5:53 pm
I would like to create a dynamic sql with sp_executesql as followed, but with errors. Please help
Do all the varchar become nvarchar ?
Thanks.
DECLARE @DatabaseName nvarchar(500)
DECLARE @TableName nvarchar(500)
DECLARE @ColumnName nvarchar(500)
DECLARE @sql nvarchar(4000)
SET @DatabaseName='Northwind'
SET @TableName='dbo.Categories'
SET @ColumnName='CategoryName'
SET @sql=''
SELECT @sql = 'USE @DatabaseName2
SELECT @ColumnName2 FROM @TableName2 '
EXEC sp_executesql @sql, N'@DatabaseName2 nvarchar(500), @ColumnName2 nvarchar(500), @TableName2 nvarchar(500)', @DatabaseName, @ColumnName, @TableName
August 29, 2010 at 9:30 pm
sqlgreenhand (8/29/2010)
I would like to create a dynamic sql with sp_executesql as followed, but with errors. Please helpDo all the varchar become nvarchar ?
Thanks.
DECLARE @DatabaseName nvarchar(500)
DECLARE @TableName nvarchar(500)
DECLARE @ColumnName nvarchar(500)
DECLARE @sql nvarchar(4000)
SET @DatabaseName='Northwind'
SET @TableName='dbo.Categories'
SET @ColumnName='CategoryName'
SET @sql=''
SELECT @sql = 'USE @DatabaseName2
SELECT @ColumnName2 FROM @TableName2 '
EXEC sp_executesql @sql, N'@DatabaseName2 nvarchar(500), @ColumnName2 nvarchar(500), @TableName2 nvarchar(500)', @DatabaseName, @ColumnName, @TableName
You can't have database names in a variable in the USE statement, or the column/table names in variables for the select statement. So, you need to build the @sql variable in this manner instead:
SET @sql = 'USE ' + QuoteName(@DatabaseName) + ';' +
'SELECT ' + QuoteName(@ColumnName) + ' FROM ' + QuoteName(@TableName) + ';';
EXEC sp_executesql @sql
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 30, 2010 at 4:40 pm
Got it! Sir.
Thanks.
August 30, 2010 at 4:57 pm
sqlgreenhand (8/30/2010)
Got it! Sir.Thanks.
No problem. Glad that I could help.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 31, 2010 at 10:23 am
This is often overlooked but for all your variables that are nvarchar it is a good practice for you to be adding the pre-pended 'N' to the hardcoded string parts being assigned to them to make them explicitly unicode. So it would look like this:
SET @sql = N'USE ' + QuoteName(@DatabaseName) + N';' +
N'SELECT ' + QuoteName(@ColumnName) + N' FROM ' + QuoteName(@TableName) + N';';
EXEC sp_executesql @sql
Since QuoteName function returns an nvarchar variable you don't need to worry about casting it to the proper type.
August 31, 2010 at 4:50 pm
Good tips.
Thanks.
August 31, 2010 at 9:19 pm
Remember, QUOTENAME is limited to 128 characters ; any input more than that will produce null.. So if the names should exceed 128 chars, i would forcefully CAST/CONVERT it to the NVARCHAR of desirable length.
From BOL :
Syntax
QUOTENAME ('character_string' [ ,'quote_character' ] )
Arguments
'character_string'
Is a string of Unicode character data. character_string is sysname and is limited to 128 characters. Inputs greater than 128 characters return NULL.
'quote_character'
Is a one-character string to use as the delimiter. Can be a single quotation mark ( ' ), a left or right bracket ( [] ), or a double quotation mark ( " ). If quote_character is not specified, brackets are used
MSDN Link : QUOTENAME
Hope this helps!
~Edit : Fixed link and added IFCodes..
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply