sqlserver中怎么导出插入脚本

sqlserver中怎么导出插入脚本,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联主要业务有网站营销策划、成都做网站、网站设计、微信公众号开发、成都小程序开发、H5技术、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、成都全网营销资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

代码如下: 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

关于sqlserver中怎么导出插入脚本问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章标题:sqlserver中怎么导出插入脚本
URL标题:http://scyanting.com/article/goeipe.html