declare kursor cursor for
select
exec_No, insert_type, stg_table, src
from
METADATA.dbo.METADATA
order by
exec_No asc;
declare @insert_type varchar(15), @stg_table varchar(30),
@src varchar(80), @SQL varchar(254), @exec_No numeric(2),
@SQL_INSERT varchar(254);
open kursor
fetch next from kursor into @exec_No, @insert_type, @stg_table, @src;
while @@FETCH_STATUS = 0
begin
if @insert_type = 'select'
begin
-- select @SQL = 'IF OBJECT_ID('''+@stg_table+''') IS NOT NULL DROP TABLE '+@stg_table;
select @SQL = 'DROP TABLE IF EXISTS '+@stg_table;
select @SQL_INSERT = 'select * from '''+@src+''' as '+@stg_table;
exec(@SQL);
execute(@SQL_INSERT);
fetch next from kursor into @exec_No, @insert_type, @stg_table, @src;
end
if @insert_type = 'bulk'
begin
execute ('truncate table ' + @stg_table);
execute('BULK INSERT '+@stg_table+'
FROM ''' + @src + '''
WITH
(
FIELDTERMINATOR = '',''
)')
end
fetch next from kursor into @exec_No, @insert_type, @stg_table, @src;
end
close kursor;
deallocate kursor;
我得到错误
第15级状态1行1的消息102
“ SOURCE.dbo.SRC_KLIENT”附近的语法不正确。第15级状态1行1的消息102
“ SOURCE.dbo.SRC_MIEJSCOWOSC”附近的语法不正确。第15级状态1行1的消息102
“ SOURCE.dbo.SRC_PRACOWNIK”附近的语法不正确。第15级状态1行1的消息102
“ SOURCE.dbo.SRC_MELDUNEK”附近的语法不正确。
错误是由语句生成的:
select @SQL_INSERT = 'select * from '''+@src+''' as '+@stg_table;
我插入的元数据:
insert into
METADATA.dbo.METADATA(exec_No,src,stg_table,temp_table, chd_table, insert_type)
values(1,'SOURCE.dbo.SRC_KLIENT', 'STAGE.dbo.STG_KLIENT', 'TEMP.dbo.TEMP_KLIENT', 'CHD.dbo.KLIENT', 'select');
insert into
METADATA.dbo.METADATA(exec_No,src,stg_table,temp_table, chd_table, insert_type)
values(15,'SOURCE.dbo.SRC_MELDUNEK', 'STAGE.dbo.STG_MELDUNEK', 'TEMP.dbo.TEMP_MELDUNEK', 'CHD.dbo.MELDUNEK', 'select');
任何想法如何使其起作用?
谢谢大家!
答案 0 :(得分:0)
我终于做到了。 有一个有效的代码(我现在知道这是不安全的):
declare
kursor cursor for select exec_No, insert_type, stg_table, src from METADATA.dbo.METADATA order by exec_No ASC;
declare @insert_type varchar(15), @stg_table varchar(30), @src varchar(80), @SQL varchar(254), @exec_No numeric(2), @SQL_INSERT varchar(254);
open kursor
fetch next from kursor
into @exec_No, @insert_type, @stg_table, @src;
WHILE @@FETCH_STATUS = 0
begin
if @insert_type = 'select'
begin
select @SQL = 'DROP TABLE IF EXISTS '+@stg_table;
select @SQL_INSERT = 'select * into ' + @stg_table + ' from ' + @src;
print(@SQL);
print(@SQL_INSERT);
exec(@SQL);
exec(@SQL_INSERT);
end
if @insert_type = 'bulks'
begin
execute ('truncate table ' + @stg_table);
execute('BULK INSERT '+@stg_table+'
FROM ''' + @src + '''
WITH
(
FIELDTERMINATOR = '',''
)')
end
fetch next from kursor
into @exec_No, @insert_type, @stg_table, @src;
end
CLOSE kursor;
deallocate kursor;
在SQL Server select * from table_1 as table_2
中,查询不正确。正确的是select * into table_2 from table_1
。
也有重复的'fetch next ...
'块(如果在其中则为第一个,在ifs中则为另一个)
感谢帮助人员!