sqlserver 導(dǎo)出插入腳本代碼
更新時間:2012年01月20日 19:57:46 作者:
工作中經(jīng)常遇到需要將遠(yuǎn)程客戶數(shù)據(jù)庫中的數(shù)據(jù)復(fù)制到本地來測試,下載整個數(shù)據(jù)庫太大了不值得,用下面的腳本可以按指定表生成Insert腳本,將腳本復(fù)制到本地來執(zhí)行,這樣快捷了不少
當(dāng)然有其它工具可以做這件事,但如果客戶不允許你在服務(wù)器亂裝東西時這個腳本就會有用了。
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
復(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
您可能感興趣的文章:
- 圖文教程mssqlserver數(shù)據(jù)庫導(dǎo)出到另外一個數(shù)據(jù)庫的方法
- SQLServer導(dǎo)出數(shù)據(jù)到MySQL實(shí)例介紹
- SQLServer導(dǎo)出sql文件/表架構(gòu)和數(shù)據(jù)操作步驟
- sqlserver bcp(數(shù)據(jù)導(dǎo)入導(dǎo)出工具)一般用法與命令詳解
- SQLServer 數(shù)據(jù)導(dǎo)入導(dǎo)出的幾種方法小結(jié)
- SQLserver 2008將數(shù)據(jù)導(dǎo)出到Sql腳本文件的方法
- 使用Navicat Premium將SQLServer數(shù)據(jù)導(dǎo)出為sql格式
相關(guān)文章
SQL Server如何通過SQL語句直接操作另一臺服務(wù)器上的SQL SERVER的數(shù)據(jù)
這篇文章主要介紹了SQL Server如何通過SQL語句直接操作另一臺服務(wù)器上的SQL SERVER的數(shù)據(jù),需要的朋友可以參考下2022-10-10詳解SQL Server 中 JSON_MODIFY 的使用
SQL Server 從 2016 開始支持了一些 JSON操作,最近的項(xiàng)目里也是好多地方字段直接存成了 JSON,需要了解一下怎么在SQL Server 中操作 JSON.這篇文章主要介紹了SQL Server 中 JSON_MODIFY 的使用,需要的朋友可以參考下2019-11-11SqlServer修改數(shù)據(jù)庫文件及日志文件存放位置
這篇文章主要介紹了SqlServer修改數(shù)據(jù)庫文件及日志文件存放位置的方法2014-07-07sqlserver服務(wù)器驗(yàn)證改為混合驗(yàn)證模式步驟
如果在安裝SQL Server數(shù)據(jù)庫時,一時疏忽使用了Windows集成驗(yàn)證方式,事后還是可以更改為混合驗(yàn)證模式的,步驟如下2013-12-12SQL Server 2016 CTP2.3 的關(guān)鍵特性總結(jié)
SQL Server2016 CTP2.2是微軟數(shù)據(jù)平臺歷史上邁出最大的一步,更快的事務(wù)處理和查詢、任何設(shè)備更深入的洞察力、更先進(jìn)的分析能力、全新安全技術(shù)和全新的混合云場景,本文給大家介紹SQL Server 2016 CTP2.3 的關(guān)鍵特性總結(jié),需要的朋友可以參考下2015-09-09Sqlserver事務(wù)備份和還原的實(shí)例代碼(必看)
下面小編就為大家?guī)硪黄猄qlserver事務(wù)備份和還原的實(shí)例代碼(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05SQL如何實(shí)現(xiàn)橫表與縱表相互轉(zhuǎn)換
針對SQL橫向表轉(zhuǎn)縱向的問題,本文從實(shí)際應(yīng)用出發(fā),詳細(xì)講解了語法和操作步驟,并結(jié)合實(shí)例進(jìn)行了演示和說明。文章還探討了該方法的優(yōu)缺點(diǎn),提出了一些值得注意的事項(xiàng),旨在幫助讀者更深入地理解這一重要的數(shù)據(jù)處理技巧2023-04-04