SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


nvarchar max problem


nvarchar max problem

Author
Message
gaurav-404321
gaurav-404321
Mr or Mrs. 500
Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)

Group: General Forum Members
Points: 587 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.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370808 Visits: 46957
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, MVP, M.Sc (Comp Sci)
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


Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)

Group: General Forum Members
Points: 105985 Visits: 9671
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)
Michael Earl-395764
Michael Earl-395764
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21727 Visits: 23078
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)

Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)SSC Guru (105K reputation)

Group: General Forum Members
Points: 105985 Visits: 9671
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?
gaurav-404321
gaurav-404321
Mr or Mrs. 500
Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)

Group: General Forum Members
Points: 587 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)))
--------------------------------------------------
svemuri-1108300
svemuri-1108300
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 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 .
aaron.reese
aaron.reese
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4092 Visits: 930
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search