sqlserver 導(dǎo)出插入腳本代碼
發(fā)布日期:2022-01-20 11:48 | 文章來(lái)源:CSDN
復(fù)制代碼 代碼如下:
DECLARE @tbImportTables table(tablename varchar(128), deleted tinyint) -- append tables which you want to import
Insert Into @tbImportTables(tablename, deleted) values('tentitytype', 1)
Insert Into @tbImportTables(tablename, deleted) values('tattribute', 1)
-- append all tables
--Insert Into @tbImportTables(tablename, deleted) select table_name, 1 from INFORMATION_SCHEMA.tables where table_type = 'BASE TABLE' DECLARE @tbImportScripts table(script varchar(max)) Declare @tablename varchar(128),
@deleted tinyint,
@columnname varchar(128),
@fieldscript varchar(max),
@valuescript varchar(max),
@insertscript varchar(max) Declare curImportTables Cursor For
Select tablename, deleted
From @tbImportTables Open curImportTables
Fetch Next From curImportTables Into @tablename, @deleted WHILE @@Fetch_STATUS = 0
Begin
If (@deleted = 1)
begin
Insert into @tbImportScripts(script) values ('Truncate table ' + @tablename)
end Insert into @tbImportScripts(script) values ('SET IDENTITY_INSERT ' + @tablename + ' ON') set @fieldscript = ''
select @fieldscript = @fieldscript + column_name + ',' from INFORMATION_SCHEMA.columns where table_name = @tablename and data_type not in('timestamp', 'image')
set @fieldscript = substring(@fieldscript, 0, len(@fieldscript)) set @valuescript = ''
select @valuescript = @valuescript + 'case when ' + column_name + ' is null then ''null'' else '''''''' + convert(varchar(max), ' + column_name + ') + '''''''' end +'',''+' from INFORMATION_SCHEMA.columns where table_name = @tablename and data_type not in('timestamp', 'image')
set @valuescript = substring(@valuescript, 0, len(@valuescript) - 4) set @insertscript = 'select ''insert into ' + @tablename + '(' + @fieldscript + ') values(' + '''+' + @valuescript + ' + '')'' from ' + @tablename
Insert into @tbImportScripts(script) exec ( @insertscript) Insert into @tbImportScripts(script) values ('SET IDENTITY_INSERT ' + @tablename + ' OFF') Insert into @tbImportScripts(script) values ('GO ')
Fetch Next From curImportTables Into @tablename, @deleted
End Close curImportTables
Deallocate curImportTables Select * from @tbImportScripts
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章