|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, March 06, 2012 3:20 AM
Points: 55,
Visits: 205
|
|
I have a stored procedure I have declared a variable of type nvarchar(max) in this. it is opening a cursor and appending some values in this string.Its not concatenating the strings beyong datalength of 7200. What can be the solution to this.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 3:37 PM
Points: 38,028,
Visits: 30,334
|
|
Can you post the code please? There are places where you would have to explicitly cast as varchar/nvarchar(max) or be cut off at 4000/8000 characters. Considering the length is 7200, it sounds like somewhere there's an implicit varchar(8000).
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP 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
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Yesterday @ 9:03 PM
Points: 21,376,
Visits: 9,581
|
|
Also you can concatenate without using the cursor like this :
DECLARE @v AS NVARCHAR(MAX) SELECT TOP 10000 @v = coalesce(@v + ', ' + M1.Name, M1.Name) FROM master.Sys.Columns M1 CROSS JOIN master.Sys.Columns M2 ORDER BY M1.Name PRINT DATALENGTH(@v) --257856 --EXEC (@v)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, August 10, 2010 5:07 AM
Points: 2,732,
Visits: 23,078
|
|
Try this:
DECLARE @v AS NVARCHAR(MAX) SELECT TOP 10000 @v = coalesce(@v + CAST(N', ' AS NVARCHAR(MAX)) + CAST(M1.Name AS NVARCHAR(MAX)), CAST(M1.Name AS NVARCHAR(MAX)) FROM master.Sys.Columns M1 CROSS JOIN master.Sys.Columns M2 ORDER BY M1.Name PRINT DATALENGTH(@v)
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Yesterday @ 9:03 PM
Points: 21,376,
Visits: 9,581
|
|
Michael Earl (9/16/2008) Try this:
DECLARE @v AS NVARCHAR(MAX) SELECT TOP 10000 @v = coalesce(@v + CAST(N', ' AS NVARCHAR(MAX)) + CAST(M1.Name AS NVARCHAR(MAX)), CAST(M1.Name AS NVARCHAR(MAX)) FROM master.Sys.Columns M1 CROSS JOIN master.Sys.Columns M2 ORDER BY M1.Name PRINT DATALENGTH(@v)
I don't see the difference with my solution Micheal, what I am missing?
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, March 06, 2012 3:20 AM
Points: 55,
Visits: 205
|
|
DECLARE @CategoryIDString nvarchar(MAX) DECLARE @ParameterIDString nvarchar2000) set @ParameterIDString ='90001,90004,90005,90006,90007,90008,90009,90010,90011,90012,90013,90014,90015,90016,90017,90018,90019,90020,90021,90022,90023,90024,90025,90026,90027,90028,90029,90030,90031,90032,90033,90034,90035,90036,90037,90038,90039,90040,90041,90042,90043,90044,90045,90046,90047,90048,90049,90050,90051,90052,90053,90054,90055,90056,90057,90058,90059,90060,90061,90062'
--FORCREATING CATEGORY STRING--------------------- SET @strquery='DECLARE CATEGORYIDCURSOR CURSOR FOR ' SET @strquery=@strquery+ 'SELECT categoryid FROM parameter WHERE parameterid in('+@ParameterIDString+')' execute sp_executesql @strquery SET @CategoryIDString='' OPEN CATEGORYIDCURSOR FETCH NEXT FROM CATEGORYIDCURSOR INTO @columnName WHILE @@FETCH_STATUS=0 BEGIN SET @CategoryIDString= CONVERT(nvarchar(max),(CONVERT(nvarchar(max),@CategoryIDString)+ convert(nvarchar(max),'SELECT [NAME] FROM category where categoryid=')+convert(nvarchar(max),@columnName) +convert(nvarchar(max),' UNION ALL '))) FETCH NEXT FROM CATEGORYIDCURSOR INTO @columnName END CLOSE CATEGORYIDCURSOR DEALLOCATE CATEGORYIDCURSOR SET @CategoryIDString=convert(nvarchar(max),(LEFT(convert(nvarchar(max),@CategoryIDString),DataLength(@CategoryIDString)-9))) --------------------------------------------------
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, March 03, 2010 6:16 PM
Points: 1,
Visits: 3
|
|
i have a similar issue. I have really a large String (docuemnt template). I have palceholders/keys inside teh template. i have a table with key and keyValue (nvarcahr(max)). i have cursor that goes through each key and replaces it with Keyvalue. Code works fine till it hists 4000 charecters . It throws error after that .
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Yesterday @ 9:53 AM
Points: 239,
Visits: 446
|
|
In the example given, @parameterID holds a list of IDs in sequence and there is no break in the sequence.
Could you resolve your issue by finding Categorys between the first and last IDs.
Alternatively, why not process the string into a temp table using string slicing and then use in (select categoryID from #temptable) instead of the long text string of @parameterID
Obiron
|
|
|
|