sqlserver 導出插入腳本代碼
更新時間:2012年01月20日 19:57:46 作者:
工作中經(jīng)常遇到需要將遠程客戶數(shù)據(jù)庫中的數(shù)據(jù)復制到本地來測試,下載整個數(shù)據(jù)庫太大了不值得,用下面的腳本可以按指定表生成Insert腳本,將腳本復制到本地來執(zhí)行,這樣快捷了不少
當然有其它工具可以做這件事,但如果客戶不允許你在服務器亂裝東西時這個腳本就會有用了。
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
復制代碼 代碼如下:
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
相關文章
SQL Server如何通過SQL語句直接操作另一臺服務器上的SQL SERVER的數(shù)據(jù)
這篇文章主要介紹了SQL Server如何通過SQL語句直接操作另一臺服務器上的SQL SERVER的數(shù)據(jù),需要的朋友可以參考下2022-10-10詳解SQL Server 中 JSON_MODIFY 的使用
SQL Server 從 2016 開始支持了一些 JSON操作,最近的項目里也是好多地方字段直接存成了 JSON,需要了解一下怎么在SQL Server 中操作 JSON.這篇文章主要介紹了SQL Server 中 JSON_MODIFY 的使用,需要的朋友可以參考下2019-11-11SqlServer修改數(shù)據(jù)庫文件及日志文件存放位置
這篇文章主要介紹了SqlServer修改數(shù)據(jù)庫文件及日志文件存放位置的方法2014-07-07SQL Server 2016 CTP2.3 的關鍵特性總結
SQL Server2016 CTP2.2是微軟數(shù)據(jù)平臺歷史上邁出最大的一步,更快的事務處理和查詢、任何設備更深入的洞察力、更先進的分析能力、全新安全技術和全新的混合云場景,本文給大家介紹SQL Server 2016 CTP2.3 的關鍵特性總結,需要的朋友可以參考下2015-09-09