----------生成自带SET IDENTITY_INSERT 的sql语句,处理不同操作系统间时间导入问题

declare @tablename varchar(256)
set @tablename='D_LawDocuType_TB'
declare @ident int
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
declare @identUp varchar(1000)
declare @identDown varchar(1000)
SELECT @identUp='',@identDown=''

select @ident=status&0x80 from syscolumns where id=object_id(@tablename) and status&0x80=0x80
if(@ident is not null)
BEGIN
 select @identUp='SELECT ''SET IDENTITY_INSERT ['+@tablename+'] ON'' UNION ALL ',
   @identDown=' UNION ALL SELECT ''SET IDENTITY_INSERT ['+@tablename+'] OFF'''
END
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'
  from
      (select case
                when 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 '+''''''''' + ' + 'CONVERT(varchar(100), '+ name +',120)'+ '+'''''''''+' 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,name

from syscolumns

where id = object_id(@tablename)

) T
set @sql =@identUp
   +'select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from ['+@tablename +']'+
   +@identDown
print @sql
exec (@sql)

----生成UPDATE语句,未经过严格测试

declare @tablename varchar(256)
set @tablename='B_TrainFlow_TB'
declare @sql varchar(8000)
declare @sqlValues varchar(8000)

set @sqlValues = ' '
select @sqlValues = @sqlValues + '[' + name + ']='' + ' + cols + ' + '','
  from
      (select case
                when 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 '+''''''''' + ' + 'CONVERT(varchar(100), '+ name +',120)'+ '+'''''''''+' 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,name

from syscolumns

where id = object_id(@tablename)
AND colid!=(select colid FROm  sysindexkeys where id = object_id(@tablename))
      ) T
--主键
select @sqlValues = left(@sqlValues,len(@sqlValues)-4) + '+'' WHERE [' + name + ']='' + ' + cols + ''
  from
      (select case
                when 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 '+''''''''' + ' + 'CONVERT(varchar(100), '+ name +',120)'+ '+'''''''''+' 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,name
  from syscolumns

where id = object_id(@tablename)
AND colid=(select colid FROm  sysindexkeys where id = object_id(@tablename))
 )T

set @sql ='select ''UPDATE ['+ @tablename + '] SET' + @sqlValues + ' from ['+@tablename +']'
print @sql
exec (@sql)

将表中的数据自动生成INSERT、UPDATE语句相关推荐

  1. html将excel数据自动导入到网页,如何把excel表中的数据自动输入到网页中

    怎么把excel表中的数据自动输入到网页中 因要给员工网上投工伤保险,员工资料在excel表中,现在在网上投保险时只能一笔笔的复杂粘贴到网页中,有没有办法能直接把excel表中的资料自动输入到网页中的 ...

  2. python亿级mysql数据库导出_Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法...

    本文实例讲述了python实现将MySQL数据库表中的数据导出生成csv格式文件的方法.分享给大家供大家参考,具体如下: #!/usr/bin/env python # -*- coding:utf- ...

  3. 使用insert向表中添加数据MySQL_使用INSERT语句向表中插入数据(MSSQLSERVER版)

    做开发的同仁对于向数据库中插入数据可谓是太普通不过了,也没什么说的,一般都是采用常用的INSERT INTO [(字段列表)] VALUES(字段值列表),这样的方式进行操作.今天,我要给大家介绍的是 ...

  4. 将sql server表中的数据导出为inert into语句的形式

    今天工作中需要用到一个中国的省市地区表,正好家里的机器中的sql server中有一个地区表.开始想直接用数据导入导出的方式把数据倒到单位的工作机器上.试了好几次都失败,没找到原因.于是想到把家里的地 ...

  5. mysql提供什么语句用于删除表中的数据_MySQL提供【 】语句用于删除表中的数据...

    MySQL提供[ ]语句用于删除表中的数据 答:暂无解析 出头教育: 当双活塞杆液压缸的缸体固定, 活塞杆与运动件固连时,其运动件的运动范 围等于液压缸有效行程的 答:三倍 可同时搜集若干调查单位资料 ...

  6. 表中的数据导出为insert语句的简单方法

    因项目需要,有时要求将一个表中的记录导出来,结合网上搜索的一些代码,而成此文 .因时间过长已找不出原创作者,谨请原谅 Create procedure pGetInsertSQL (@tablenam ...

  7. 解决白盒、mock、性能、自动化测试脚本中的数据自动生成问题——Spock-Genesis

    文章目录 前言 一.Spock-Genesis是什么? 二.使用步骤 1.添加Spock-Genesis依赖 2.示例 总结 前言 程序的基本功能就是通过输入一组数据,得出我们期望的输出.我们测试的目 ...

  8. 【excel数据转脚本批量插入数据库】将excel中的数据快速生成insert脚本

    1.excel快速批量生成insert语句 打开excel,在空白的列插入以下函数 ="INSERT INTO TABLENAME(UserId,UserName,UserPwd) VALU ...

  9. SQL Server 数据库中将数据表中的数据转化为Insert 语句

    Use Northwind Select 'Insert Into Customer(CustomerID,CompanyName,ContactName)  Values ('+'''' +Cust ...

最新文章

  1. android平台 arcgisr_ArcGIS Runtime For Android 开发 (7)
  2. IOS 编程中引用第三方的方类库的方法及常见问题
  3. android 单个模块编译的方法
  4. Hbase 表名修改
  5. 程序员必会的核心基础知识:1张导图+10本书
  6. Spring MVC中@ControllerAdvice注解实现全局异常拦截
  7. bug3-自定义层的注意事项
  8. 1000套微信小程序源码模板分享下载,各种类型任意选择
  9. GitHub上整理的一些工具[转载]
  10. linux安装为知笔记本,Scientific Linux 安装 wiznote(为知笔记)
  11. 天猫旺铺装修的一些特殊代码 【持续收集中...】
  12. java实现文件下载功能
  13. E - Help Hanzo(LightOJ 1197)
  14. Java解压压缩加密文件zip
  15. HTML5 canvas热点图应用
  16. Pytorch官网一直很卡进不去,离线下载pytorch各类版本安装包方法
  17. tikz 折线 箭头_TIKZ——LaTeX基本绘图
  18. 迭代器模式(lterator Pattern)简介
  19. MongoDB使用中的一些问题
  20. LAPACK的C/C++接口及代码实例

热门文章

  1. 前端学习(2588):前端权限的控制思路
  2. 前端学习(2135):ES模块化的导出和导入
  3. 前端学习(1859)vue之电商管理系统电商系统之梳理项目结构
  4. 前端学习(381):CSS3 的视口单位vw、vh实现自适应(带有px,em,rem的简单介绍)
  5. Git学习(3)GitHub和SVN的区别
  6. 第二十九期:运维之三大监控对比
  7. lib60870-IEC 60870-5-101 / 104 协议对总查询的处理
  8. Qt中的TableWidget初始化表头、行高、选中、自动扩展和接受修改
  9. TCP服务器epoll的多种实现
  10. idea命令行运行多个客户端_推荐一款神仙颜值的 Redis 客户端工具,开源啦