0

I have read about a dozen articles here and I am still stumped with this issue.

I am building a dynamic select statement that will update a view on a monthly schedule.

set ansi_nulls on
go
set quoted_identifier on
go

alter procedure [dbo].[Proc_Name_SP]

as
begin
set nocount on
set quoted_identifier off

declare @dbname varchar(10), @schema_id int, @schema_name varchar(10),
@jacro varchar(10), @rec_cnt int, @tot_rec int
declare @SQL_Main nvarchar(max), @SQL_Final nvarchar(max),
@SQL_schema nvarchar(2000), @SQL_Union nvarchar(max)

declare iteration cursor global static for
    -- Begin statement for cursor array
    select distinct db, code 
    from linkedserver.db.schema.Directory
    where current_stage = 'live'    
    order by db
    -- End statement for cursor array

-- get total number of cursor iterations to know when to stop
-- "union" statements
select @tot_rec = count(*) from (select distinct db, code 
                from  [linkedserver].db.schema.Directory
                where current_stage = 'live') as cur
    -- begin loop               
    open iteration
        fetch first from iteration into @dbname, @jacro
        while @@fetch_status=0 
        begin
-- the schema used is not consistent. Because of the linked server it was
-- necessary to get the Schema_ID from the sys.tables and then pull the 
-- schema name from sys.schema
    set @SQL_schema = 'select @sch_id = schema_id from [linkedserver].'+@dbname+'.sys.tables where name = ''Manuscript'''
    execute sp_executesql @SQL_schema, N'@sch_id int OUTPUT', @sch_id = @schema_id output
    --print @schema_id          
    set @SQL_schema ='select @sch_name = name from [linkedserver].'+@dbname+'.sys.schemas where schema_id = '+cast(@schema_id as varchar)+''
    execute sp_executesql @SQL_schema, N'@sch_name nvarchar(10) OUTPUT',  @sch_name = @schema_name output
    --print @schema_name

    --building Select statement
            set @SQL_Main ='
    select jcode.Code as BILLING_ACRO
    ,s.start_dt as BILLING_DATE
    ,cmpt_ms_nm as MANUSCRIPT
    ,isnull(jcode.billing_type, ''reviewed'') as Billing_type 
    from [linkedserver].'+@dbname+'.'+@schema_name+'.Manuscript as m
    join [linkedserver].'+@dbname+'.'+@schema_name+'.Step as s on m.ms_id = s.ms_id and m.ms_rev_no = s.ms_rev_no
    join (select j_id, Code, billing_type from [linkedserver].db.schema.Directory where db = '''+@dbname+''') as                                                                      jcode on jcode.j_id = m.j_id
    where jcode.Code = '''+@jacro+'''
    and m.ms_rev_no = 0 
    and s.stage_id = 190
    and isnull(cmpt_ms_nm, '''') <> ''''
    and s.step_id = (select min(s2.step_id) 
    from [linkedserver].'+@dbname+'.'+@schema_name+'.Step as s2 
    where s2.stage_id = 190
    and s2.ms_id = m.ms_id
    and s2.ms_rev_no = m.ms_rev_no)
                    '                            
        set @rec_cnt = isnull(@rec_cnt, 0) + 1
        if @SQL_Union is null
        begin
        set @SQL_Union = @SQL_Main
        end
        else if @tot_rec <> @rec_cnt
        begin 
        set @SQL_Union = @SQL_Union + ' union ' + @SQL_Main
        end 
        else
        begin
        set @SQL_Union = @SQL_Union + @SQL_Main
        end 
        --print @rec_cnt
        fetch next from iteration into @dbname, @jacro  --next database

        end -- while @@FETCH_STATUS=0

close iteration

deallocate iteration


-- build new view
print len(@SQL_Union)
set @SQL_Final = '
ALTER VIEW [dbo].[View_Name_VW]
AS
'+@SQL_Union+'
'
execute sp_executesql @SQL_Final

--grab string variables to table for troubleshooting
insert into Output_SQL(SQL_Final, SQL_Final_Len, SQL_Union, SQL_Union_Len)
select @SQL_Final, LEN(@SQL_Final), @SQL_Union, LEN(@SQL_Union)


set nocount off
end
go

I have read that others have had problems with this type of truncation and I have tried multiple suggestions but in the end the I am getting capped at 68274 in this code with nvarchar(max). For troubleshooting, I am saving the results of the variables and the len of these variables to a table to eliminate the SSMS cap on the display of strings.

I have tried cast(@varible as nvarchar(max)) on the right side of the = sign. I have changed the data type lengths (as the select that is being built is not that large, it is just large after it has been union for each unique customer)

I am open to any suggestions as I have tried many variations of datatype declarations for these variables.

Jeffry L.
  • 21
  • 3
  • 1
    What is the error you are getting? The reason ask is because this is likely a problem: "set atSQL_Union = atSQL_Union + atSQL_Main" You are missing a UNION. – Alex Jun 04 '16 at 07:28
  • Thank you for a response. As I mentioned in my post, I my data is getting truncated at 68k characters even though I have used nvarchar(max). The error I am receiving is simple and related to the Select being cut-off. That block of if... else code that handles when the "union" statement is added to the select. The problem is the last select cannot have the union. Also, you cannot concatenate to an empty variable thus the first part of the logic. – Jeffry L. Jun 06 '16 at 16:49
  • Please post the error messge. Also follow this link http://stackoverflow.com/questions/7850477/how-to-print-varcharmax-using-print-statement to "print" the dynamic SQL and check that it has no syntax errors – Alex Jun 06 '16 at 23:24
  • Alex, you were absolutely correct. I went back and read what you posted and it finally clicked. Thank you very much. – Jeffry L. Jun 07 '16 at 19:34

0 Answers0