nvarchar max problem

  • 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.

  • 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
  • Also you can concatenate without using the cursor like this :

    DECLARE @v-2 AS NVARCHAR(MAX)

    SELECT TOP 10000 @v-2 = 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)

  • Try this:

    [font="Courier New"]DECLARE @v-2 AS NVARCHAR(MAX)

    SELECT TOP 10000 @v-2 = 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)[/font]

  • Michael Earl (9/16/2008)


    Try this:

    [font="Courier New"]DECLARE @v-2 AS NVARCHAR(MAX)

    SELECT TOP 10000 @v-2 = 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)[/font]

    I don't see the difference with my solution Micheal, what I am missing?

  • 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)))

    --------------------------------------------------

  • 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 .

  • 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

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply