Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2007-03-07 : 08:38:58
|
Maryam writes "HiThanx for your useful comment about dynamic SQL. it works out for me, and gaint my attention.Although it was complete, cause another question:How I can make a long string. I mean I used sp_executesql witha variable of nvarchar. but this data type has maximum length of 4,000 characters. And my Query string is much more..I tried to Declare variable of ntext,text but SQL doesen't let for local variable.Then I used char, varchar but again when I used the variable in front of sp_executesql, it caused error.So Could you help me and say How to set a long string for Dynamic SQL?ThanxI look forward your answer" |
|
rlaubert
Yak Posting Veteran
96 Posts |
Posted - 2007-03-08 : 11:56:17
|
Best bet is to concatonate strings. Something likein a stored procedure it could be that these values are passed during the call.Declare @@str1 nvarchar(4000), @@str2 nvarchar(4000) -- etc. as needed-- set initial values for each stringSet @@str1 = ''Set @@str2 = ''-- Populate strings with actual valuesset @@str1 = 'string values go here'set @@str2 = 'string values go here'if @@str2 is nullRaymond LaubertMCDBA, MCITP:Administration, MCT |
 |
|
rlaubert
Yak Posting Veteran
96 Posts |
Posted - 2007-03-08 : 11:56:51
|
sorry.. here is the restIF @@str2 is nullRaymond LaubertMCDBA, MCITP:Administration, MCT |
 |
|
rlaubert
Yak Posting Veteran
96 Posts |
Posted - 2007-03-08 : 11:56:51
|
sorry.. here is the restIF @@str2 is nullRaymond LaubertMCDBA, MCITP:Administration, MCT |
 |
|
rlaubert
Yak Posting Veteran
96 Posts |
Posted - 2007-03-08 : 11:56:52
|
sorry.. here is the restIF @@str2 is nullRaymond LaubertMCDBA, MCITP:Administration, MCT |
 |
|
rlaubert
Yak Posting Veteran
96 Posts |
Posted - 2007-03-08 : 11:58:20
|
Try again..if @@str2 is nullexec sp_executesql @@str1elseexec sp_executesql @@str1 + @@str2Havnet tried this but should work.Raymond LaubertMCDBA, MCITP:Administration, MCT |
 |
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-08 : 13:24:49
|
quote: Originally posted by rlaubert Try again..if @@str2 is nullexec sp_executesql @@str1elseexec sp_executesql @@str1 + @@str2Havnet tried this but should work.
Nope, that won't work, you'll need to use EXECUTE instead because you cannot give expressions to sp_executesqlif @str2 is nullexec (@str1)elseexec (@str1 + @str2) Raymond - Any reason why you use two @ signs for your variables? It seems confusing because @@ is used by the system for functions? |
 |
|
rlaubert
Yak Posting Veteran
96 Posts |
Posted - 2007-03-08 : 16:05:26
|
double @ is global... just got use to using them lately with stuff I am doing at work...You are right on using single though.Raymond LaubertMCDBA, MCITP:Administration, MCT |
 |
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-08 : 16:17:17
|
quote: Originally posted by rlaubert double @ is global... just got use to using them lately with stuff I am doing at work...You are right on using single though.Raymond LaubertMCDBA, MCITP:Administration, MCT
I thought you might think that - they aren't global, they used to be referred to as global variables (prior to version 7.0), but they were never global variables, they were global system values, and were renamed functions in version 7.0. So @@version and @@identity are now system functions. You can declare a variable with two @ signs in your code, but it's not global, it's no different to a variable one @ sign (in fact go ahead and put 15 @ signs if you like). I'd say don't do it because if you do you run the risk of someone thinking it is something it isn't.This is from the 2000 Books OnlinePre-SQL Server 7.0 SQL Server 7.0 Global variables were system-supplied, predeclared variables that were distinguished from local variables by having two at symbols (@@) preceding their names. SQL Server 7.0Transact-SQL global variables are a form of function and are now referred to as functions. |
 |
|
|