當(dāng)然有其它工具可以做這件事,但如果客戶不允許你在服務(wù)器亂裝東西時(shí)這個(gè)腳本就會(huì)有用了。
復(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)出到另外一個(gè)數(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格式