我正在sql server中编写一个sql脚本,它根据数据创建动态表。 我最初收到错误“对象名称'GBIPS-I-DB324D.CoreReferenceStaging.dbo.FinData2000_1'包含的最大前缀数量。最大值为2”。在运行原始查询时。 GBIPS-I-DB324D是链接服务器对象。
我尝试使用execute语句,现在在关键字“USE”附近收到错误语法错误。
原始查询
BEGIN
set nocount on
declare @startYear int, @startQuarter int, @sql nvarchar(max), @tableName varchar(50);
set @startYear = 2000;
set @startQuarter = 1;
while(@startYear < 2018)
begin
set @startQuarter = 1;
while(@startQuarter < 5)
begin
set @tableName = 'FinData' + cast(@startYear as varchar) + '_' + cast(@startQuarter as varchar);
set @sql = '
drop table [GBIPS-I-DB324D].[CoreReferenceStaging].[dbo].[' + @tableName + '];
create table [GBIPS-I-DB324D].[CoreReferenceStaging].[dbo].[' + @tableName + '] ( calendarYear int, calendarQuarter int, companyid bigint not null, dataitemid bigint not null, dataitemvalue numeric(28,6), fiscalyear int, fiscalquarter int, periodenddate datetime, filingdate datetime, latestforfinancialperiodflag bit, latestfilingforinstanceflag bit );
insert into [GBIPS-I-DB324D].[CoreReferenceStaging].[dbo].[' + @tableName + ']
select fp.calendarYear, fp.calendarQuarter, fp.companyid, fd.dataitemid, fd.dataitemvalue, fp.fiscalyear, fp.fiscalquarter, fi.periodenddate, fi.filingdate, fi.latestforfinancialperiodflag, fi.latestfilingforinstanceflag
from [Xpressfeed_dev].[dbo].[ciqFinPeriod] fp
inner join [Xpressfeed_dev].[dbo].[ciqFinInstance] fi on fi.financialPeriodId = fp.financialPeriodId
inner join [Xpressfeed_dev].[dbo].[ciqFinInstanceToCollection] fc on fc.financialInstanceId = fi.financialInstanceId
inner join [Xpressfeed_dev].[dbo].[ciqFinCollection] c on c.financialCollectionId = fc.financialCollectionId
inner join [Xpressfeed_dev].[dbo].[ciqFinCollectionData] fd on fd.financialCollectionId = c.financialCollectionId
where YEAR(fi.periodenddate) = ' + cast(@startYear as varchar) + ' and QUARTER(fi.periodenddate) = ' + cast(@startQuarter as varchar)
EXEC sp_executesql @sql
set @startQuarter += 1
end
set @startYear += 1;
end
end
修改后的查询
BEGIN
set nocount on
declare @startYear int, @startQuarter int, @sql nvarchar(max), @tableName varchar(50);
set @startYear = 2000;
set @startQuarter = 1;
while(@startYear < 2018)
begin
set @startQuarter = 1;
while(@startQuarter < 5)
begin
set @tableName = 'FinData' + cast(@startYear as varchar) + '_' + cast(@startQuarter as varchar);
set @sql = '
EXECUTE (USE CoreReferenceStaging;drop table [dbo].[' + @tableName + ']) AT [GBIPS-I-DB324D]
EXECUTE (USE CoreReferenceStaging;create table [dbo].[' + @tableName + '] ( calendarYear int, calendarQuarter int, companyid bigint not null, dataitemid bigint not null, dataitemvalue numeric(28,6), fiscalyear int, fiscalquarter int, periodenddate datetime, filingdate datetime, latestforfinancialperiodflag bit, latestfilingforinstanceflag bit )) AT [GBIPS-I-DB324D]
insert into [GBIPS-I-DB324D].[CoreReferenceStaging].[dbo].[' + @tableName + ']
select fp.calendarYear, fp.calendarQuarter, fp.companyid, fd.dataitemid, fd.dataitemvalue, fp.fiscalyear, fp.fiscalquarter, fi.periodenddate, fi.filingdate, fi.latestforfinancialperiodflag, fi.latestfilingforinstanceflag
from [Xpressfeed_dev].[dbo].[ciqFinPeriod] fp
inner join [Xpressfeed_dev].[dbo].[ciqFinInstance] fi on fi.financialPeriodId = fp.financialPeriodId
inner join [Xpressfeed_dev].[dbo].[ciqFinInstanceToCollection] fc on fc.financialInstanceId = fi.financialInstanceId
inner join [Xpressfeed_dev].[dbo].[ciqFinCollection] c on c.financialCollectionId = fc.financialCollectionId
inner join [Xpressfeed_dev].[dbo].[ciqFinCollectionData] fd on fd.financialCollectionId = c.financialCollectionId
where YEAR(fi.periodenddate) = ' + cast(@startYear as varchar) + ' and QUARTER(fi.periodenddate) = ' + cast(@startQuarter as varchar)
EXEC sp_executesql @sql
set @startQuarter += 1
end
set @startYear += 1;
end
end
答案 0 :(得分:0)
据我所知,&#39; USE&#39;使用链接服务器时无法使用语句。这不能回答你关于2的最大值的问题但是&#39;使用&#39;不是要走的路。当游戏中没有链接服务器时,您可以使用&#39; USE&#39;,但在执行DML操作的同一语句中构建use-statement。您必须使用动态SQL来处理具有副作用的动态SQL