July 18, 2008 at 11:58 am
What happens to nvarchar variables when different concatenations are performed. (This is for a dynamic sql call).
Given the following code:
DECLARE @sql nvarchar(MAX)
DECLARE @where nvarchar(MAX)
SET @where = ' WHERE Name LIKE ''%' + @Name + '%'' '
SET @sql = 'SELECT * FROM Customers' + @where
exec sp_executesql @sql
Question 1:
Since @where and @sql are both nvarchar(MAX) what issues might arise with string size and overflowing?
Question 2:
I didn't prefix each string with N to designate it as an nvarchar string. What issues might this cause?
Question 3:
Is this the more correct code? This can get ugly if your @sql has lots of different variables concatenated into it. Is every N prefix necessary?
DECLARE @sql nvarchar(MAX)
DECLARE @where nvarchar(MAX)
SET @where = N' WHERE Name LIKE ''%' + @Name + N'%'' '
SET @sql = N'SELECT * FROM Customers' + @where
exec sp_executesql @sql
Question 4:
If I omit one N prefix what will the result be? For example (notice the missing N at the end):
SET @where = N' WHERE Name LIKE ''%' + @Name + '%'' '
I really appreciate any answers. Thanks!
July 18, 2008 at 12:21 pm
Have you taken the samples you provided, and run them in your database on your dev server?
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
July 18, 2008 at 1:08 pm
Yes it does return data but what will happen if a unicode character makes its way into one of the variables?
Imagine we have @var nvarchar(MAX) and it contains unicode data. Will it remain unicode (nvarchar) if its concatenated with a non unicode string.
@var + 'non unicode'
What is the result of the above statement? And what is the difference if we use the following statement instead:
@var + N'non unicode'
Thanks.
July 18, 2008 at 1:59 pm
Well, I hadn't played with that, so I tested it.
declare @NV nvarchar(max), @v-2 varchar(max)
select @NV = NChar(0641), @v-2 = 'a'
select @NV, @v-2
select @NV = @NV + @v-2
select @NV
The result of the concatenation is ?a, which is exactly what it should be.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
July 18, 2008 at 2:47 pm
Interesting. I've done a bunch more testing here and it seems the only time you need to prefix a literal string with N is when the literal string itself contains unicode characters.
July 18, 2008 at 4:09 pm
Please read BoL topics "Implicit Conversions" and "Data Type Precedence" that define the rules, such that " N'a' + 'b' " is internally converted into " N'a' + cast('b' as nchar(1) ) ". That implicit data type conversion occurs is often the cause of performance problems.
Implicit conversions are those conversions that occur without specifying either the CAST or CONVERT function. Explicit conversions are those conversions that require the CAST or CONVERT function to be specified.
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type.
SQL = Scarcely Qualifies as a Language
July 19, 2008 at 12:38 am
whatispunk (7/18/2008)
What happens to nvarchar variables when different concatenations are performed. (This is for a dynamic sql call).Given the following code:
DECLARE @sql nvarchar(MAX)
DECLARE @where nvarchar(MAX)
SET @where = ' WHERE Name LIKE ''%' + @Name + '%'' '
SET @sql = 'SELECT * FROM Customers' + @where
exec sp_executesql @sql
Hi whatispunk!
For this specific example you would not need dynamic sql, you could use this instead:
DECLARE @param nvarchar(MAX);
SET @param = N'%' + @Name + N'%';
SELECT * FROM dbo.Customers WHERE Name LIKE @param;
Best Regards,
Chris Büttner
July 19, 2008 at 6:04 pm
Ya beat me to it, Chris. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
July 21, 2008 at 1:50 pm
Just luck 😉
Best Regards,
Chris Büttner
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply