我在这里读了十几篇文章,我仍然对这个问题感到难过。
我正在构建一个动态选择语句,它将按月计划更新视图。
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
我已经读过其他人在这种类型的截断方面遇到了问题,我尝试了多个建议,但最后我在这段代码中使用nvarchar(max)获得了68274的上限。为了排除故障,我将变量的结果和这些变量的len保存到表中,以消除字符串显示上的SSMS上限。
我在=符号的右侧尝试了强制转换(@varible as nvarchar(max))。我已经更改了数据类型的长度(因为正在构建的选择不是那么大,在为每个唯一客户建立联合之后它只是很大)
我对任何建议持开放态度,因为我已经为这些变量尝试了许多数据类型声明变体。