Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

nvarchar max problem Expand / Collapse
Author
Message
Posted Tuesday, September 16, 2008 9:33 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued 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.

Post #570370
Posted Tuesday, September 16, 2008 11:01 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 12:20 PM
Points: 41,529, Visits: 34,445
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

Post #570458
Posted Tuesday, September 16, 2008 11:25 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, January 16, 2014 12:03 PM
Points: 21,376, Visits: 9,594
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)
Post #570481
Posted Tuesday, September 16, 2008 12:56 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

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)
Post #570542
Posted Tuesday, September 16, 2008 1:36 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Thursday, January 16, 2014 12:03 PM
Points: 21,376, Visits: 9,594
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?
Post #570572
Posted Tuesday, September 16, 2008 9:57 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued 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)))
--------------------------------------------------
Post #570730
Posted Wednesday, March 03, 2010 11:45 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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 .

Post #876245
Posted Wednesday, March 03, 2010 2:24 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, April 10, 2014 3:49 PM
Points: 366, Visits: 577
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
Post #876361
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse