July 5, 2009 at 4:37 pm
Guys I made the variable @nsql nvarchar(max) to be used for dynamic sql. When I execute using sp_executesql, it throws me error and seems like the sql that it was trying to run was incomplete (4000 chars only and it is where it throws error).... I was able to use workaround by using two variables and using
Exec(@var1 + @var2) ... But not sure why would nvarchar(max) not work?
Anyone going through this issue? I am using sql server 2005 enterprise edition... thanks!
July 5, 2009 at 8:48 pm
sp_executesql, even though its an "internal" stored procedure, is still a proc, and it has a parameter that is an nvarchar(4000);
EXEC is a command, and has no parameters.. just tries to execute whatever you pass it. So when you pass it a (max) or bunch of (max)'s concatened, it works fine, where with sp_executesql, it gets truncated.
Lowell
July 5, 2009 at 9:00 pm
Lowell (7/5/2009)
...EXEC is a command, and has no parameters.. just tries to execute whatever you pass it. So when you pass it a (max), it gets truncated.
??? I'm sure this must be some kind of typo, Lowell, but just to be clear, EXEC(NVARCHAR(max)) will *not* truncate the string, it will execute the whole thing.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
July 5, 2009 at 9:12 pm
Ghanta (7/5/2009)
Guys I made the variable @nsql nvarchar(max) to be used for dynamic sql. When I execute using sp_executesql, it throws me error and seems like the sql that it was trying to run was incomplete (4000 chars only and it is where it throws error).... I was able to use workaround by using two variables and usingExec(@var1 + @var2) ... But not sure why would nvarchar(max) not work?
Anyone going through this issue? I am using sql server 2005 enterprise edition... thanks!
Can you post the procedure? Normally this works, so there must be some additional factor.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
July 5, 2009 at 9:12 pm
definitely fatfingered...i meant to say EXEC will use all you pass it, whether concatenated (max) variables or whatever, where sp_executesql is the one that will truncate at 4000; thanks, and i fixed my post...i know better, just a disconnect in the chair-to-keyboard-interface.
Lowell
July 5, 2009 at 9:26 pm
Thing is, I'm not sure that its true about sp_ExecuteSql either. Here's a script that demonstrates both successfully executing a string WAY over 4000 characters:
Declare @sql as nvarchar(MAX)
set @sql = ''
IF '''+name+N'''=''not in there'' PRINT ''Found It!'';'
from master.sys.system_columns
IF ''not in there''=''not in there'' PRINT ''Found It on the last line!'';'
Print len(@sql)
EXEC (@sql)
EXEC sp_ExecuteSql @sql
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
July 5, 2009 at 10:38 pm
Ghanta (7/5/2009)
Guys I made the variable @nsql nvarchar(max) to be used for dynamic sql. When I execute using sp_executesql, it throws me error and seems like the sql that it was trying to run was incomplete (4000 chars only and it is where it throws error).... I was able to use workaround by using two variables and usingExec(@var1 + @var2) ... But not sure why would nvarchar(max) not work?
Anyone going through this issue? I am using sql server 2005 enterprise edition... thanks!
Is it possible that it's running through some old sql 2000 code first that hasn't been upgraded to use nvarchar(max)? Sql 2000 has a varchar(8000)/nvarchar(4000) limit, and this would truncate your string if it was passed to it.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
July 6, 2009 at 12:14 am
WayneS (7/5/2009)
Ghanta (7/5/2009)
Guys I made the variable @nsql nvarchar(max) to be used for dynamic sql. When I execute using sp_executesql, it throws me error and seems like the sql that it was trying to run was incomplete (4000 chars only and it is where it throws error).... I was able to use workaround by using two variables and usingExec(@var1 + @var2) ... But not sure why would nvarchar(max) not work?
Anyone going through this issue? I am using sql server 2005 enterprise edition... thanks!
Is it possible that it's running through some old sql 2000 code first that hasn't been upgraded to use nvarchar(max)? Sql 2000 has a varchar(8000)/nvarchar(4000) limit, and this would truncate your string if it was passed to it.
There are a couple of flukey ways that an NVarchar(MAX) can get cut back to 4000 characters in the middle of a procedure, but I cannot remember exactly what they are. I'm pretty sure that I can figure it out if see the code though.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
July 6, 2009 at 12:25 am
Of course I'm going on Vacation in the morning... So, you want to look for things like adding string literals onto the front of your NVarchar(MAX):
SELECT @BigStr = 'Some stuff ...' + @BigStr
There's some scenario like this where the intermediate string gets cut down to 4000 characters, but I cannot remember the exact conditions.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
July 6, 2009 at 8:27 am
.
July 6, 2009 at 10:43 am
what does those SQL variables content?
there are sometimes that you can split the ddl into multiple operations, maybe Adding columns one by one to a table, or converting long queries in Views, in not sure how to make EXEC to run bigger queries but there should be another way to "compress" the query.
this is just in case you want to use only one variable instead of @var1+ @var2
regards
July 25, 2009 at 10:09 pm
Guys thanks for all the info... definitely helped me to understand few things...
My issue was solved by updating the compatibility level I think..
exec sp_dbcmptlevel 'dbname', 90
July 26, 2009 at 3:01 pm
That would certainly do it. Just watch for problems on that if the old database was 80 compatible...
--Jeff Moden
Change is inevitable... Change for the better is not.
July 29, 2009 at 5:22 am
hi Rbarry
please see the below loink's problem
http://www.sqlservercentral.com/Forums/FindPost761384.aspx
thanks
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
August 17, 2009 at 8:43 am
This behavior really threw me out in cold for a while! After going through this thread I broke the dynamic sql in multi-part and it worked fine. Yes, my dynamic SQL was 4194 characters but the SQL will truncate it to 3986 characters (very near to 4k). And I was using EXEC to execute the SQL. Even a "print" command would not emit the entire SQL (the BOL says it can take upto 8000 chars).
Upon digging deeper into my dynamic SQL I found that one of my variables concatenated in the dynamic SQL was of type NVARCHAR. Changing it to VARCHAR made my dynamic SQL work fine.
The only thing that still bugs me is why does it work when I break it multi-part but not when its a single string!:ermm:
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply