sqlserver 导出插入脚本代码建站知识
导读:1建站知识工作中经常遇到需要将远程客户数据库中的数据复制到本地来测试,下载整个数据库太大了不值得,用下面的脚本可网站建设教程网站seo优化软件。
当然有其它工具可以做这件事,但如果客户不允许你在服务器乱装东西时这个脚本就会有用了。
复制代码 代码如下:
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 Inse网站建设公司rt 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) valu网站建设教程es ('GO ') Fetch Next From curImportTables Into @tablename, @deleted End Close curImportTables Deallocate curImportTables Select * from @tbImportScripts
相关网站建设教程网站seo优化软件。声明: 本文由我的SEOUC技术文章主页发布于:2023-05-22 ,文章sqlserver 导出插入脚本代码建站知识主要讲述脚本,标签,sqlserver 导出插入脚本代码建站知识1网站建设源码以及服务器配置搭建相关技术文章。转载请保留链接: https://www.seouc.com/article/web_4281.html