sqlserver 导出插入脚本代码

所属分类: 数据库 / MsSql 阅读数: 1332
收藏 0 赞 0 分享
当然有其它工具可以做这件事,但如果客户不允许你在服务器乱装东西时这个脚本就会有用了。
复制代码 代码如下:

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 Server的聚焦索引和非聚集索引进行简单分析,从而总结出聚焦索引对非聚集索引的影响。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅析SQL Server的聚焦使用索引和查询执行计划

本文通过介绍默认使用索引、强制使用聚集索引、强制使用非聚集索引让我们知道对于检索所有列结果集使用主键的聚集索引是最佳选择。有兴趣的朋友可以看下
收藏 0 赞 0 分享

详解SQL Server中的数据类型

本文主要讲解了SQL中的数据类型以及几个需要注意的地方,简短的内容,深入的理解。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅析SQL Server的分页方式 ISNULL与COALESCE性能比较

本文上述重点讲述了SQL Server的分页方式,COALESCE和ISNULL函数区别之处,简短的内容,深入的理解。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅述SQL Server的聚焦强制索引查询条件和Columnstore Index

本文主要讲了强制使用索引条件来进行查询,当对于使用默认创建索引进行查询计划时觉得不是最优解,可以尝试使用强制索引来进行对比找出更好得解决方案。简短的内容,深入的理解.有兴趣的朋友可以看下
收藏 0 赞 0 分享

详解SQL Server的聚焦过滤索引

本文主要讲解了通过过滤索引来提高查询性能,同时也给出了其不同的场景以及其使用优点和明显的缺点。简短的内容,深入的理解,有兴趣的朋友可以看下
收藏 0 赞 0 分享

解析SQL Server聚焦移除(Bookmark Lookup、RID Lookup、Key Lookup)

本文主要讲解索引性能优化,着重对Bookmark Lookup、RID Lookup、Key Lookup三者进行移除的实现进行解析,以此来提高查询性能。希望对大家有所帮助
收藏 0 赞 0 分享

SqlServer 注释符 单行注释与多行注释

这篇文章主要介绍了SqlServer 注释符 单行注释与多行注释,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL设置SQL Server最大连接数及查询语句

今天遇到了关于Sql Server最大连接数(Max Pool Size)的问题,后来通过查找一些资料解决了,所以想着总结下关于SQL Server最大连接数的内容,所以这篇文章主要介绍了SQL设置SQL Server最大连接数与查询语句,有需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

浅谈SQL Server交叉联接 内部联接

本文主要讲了SQL Server的交叉联接和内部联接,同时也给出了使用需要注意的地方。有需要的朋友可以看下
收藏 0 赞 0 分享
查看更多