博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
导出表中数据为insert语句
阅读量:7055 次
发布时间:2019-06-28

本文共 1956 字,大约阅读时间需要 6 分钟。

alter procedure pGetInsertSQL (@tablename varchar(256))---WITH ENCRYPTIONasbegin---得到表中的SQL -- declare @tablename varchar(256) -- set @tablename = 'Design_DBPageStruct' --Create Table SQLtmp --( --sql varchar(4000) --)---Delete  from SQLtmpif not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SQLtmp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)Create Table SQLtmp(sql varchar(4000))declare @sql varchar(4000)declare @sqlValues varchar(4000)set @sql =' ('set @sqlValues = 'values (''+'select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'from(select casewhen xtype in (48,52,56,59,60,62,104,106,108,122,127)then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'when xtype in (58,61)then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'when xtype in (167)then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'when xtype in (231)then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'when xtype in (175)then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'when xtype in (239)then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar)  + '))+'''''''''+' end'else '''NULL'''end as Cols,namefrom syscolumnswhere id = object_id(@tablename)) Tset @sql ='insert into SQLtmp (sql) select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ');'' from '+@tablename + '  '--  print @sql  --exec (@sql)Select sql from SQLtmp---Select sql from SQLtmp  FOR XML AUTOendgo

转载地址:http://lvlol.baihongyu.com/

你可能感兴趣的文章
NodeJS发送邮件
查看>>
再谈Js闭包
查看>>
C++ 打造 Markdown 解析器
查看>>
nodejs微信开发---接入指南
查看>>
如何理解Kubernetes认证和授权
查看>>
1625行,解开 underscore.js 的面纱 - 第四章
查看>>
一些正则
查看>>
iOS动画编程-Layer动画[ 5 ] Animation Groups组合动画
查看>>
mysql & redis 性能测试
查看>>
Python下正则表达式学习笔记
查看>>
node.js express jquery ajax post options 跨域问题 解决办法
查看>>
谈 DevOps 自动化时,也应该考虑到 SOX 等法案
查看>>
git终极指南:在实际开发中的应用
查看>>
阿里云服务器怎么重装系统?
查看>>
锌财经3月科技主题沙龙丨袋鼠云-云掣CEO徐进挺(丁原)受邀探讨“未来智能时代下的行业生存机会” ...
查看>>
【镜像更新】Windows Server 2012 R2 数据中心版
查看>>
日志服务Python消费组实战(三):实时跨域监测多日志库数据 ...
查看>>
网站被黑跳转到其他网站的解决办法
查看>>
填报脚本之轻松搞定复杂表的数据入库
查看>>
HttpClient在多线程环境下踩坑总结
查看>>