≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > SQL Server

SQL Server 将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本

人气:471 时间:2019-05-14

这篇文章主要为大家详细介绍了SQL Server 将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

代码如下:


set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 
go 
-- ============================================= 
-- Author: 华岭 
-- Create date: 2008-10-28 
-- Description: 将表数据生成Insert脚本 
-- Demo : exec pCreateInsertScript 'BexmCodeType','dictypeid = 61' 
-- ============================================= 
alter proc [dbo].pCreateInsertScript (@tablename varchar(256),@con nvarchar(400)) 
as 
begin 
set nocount on 
declare @sqlstr varchar(4000) 
declare @sqlstr1 varchar(4000) 
declare @sqlstr2 varchar(4000) 
select @sqlstr='select ''insert '+@tablename 
select @sqlstr1='' 
select @sqlstr2='(' 
select @sqlstr1='values (''+' 
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case 
when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end' 
when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 
when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 
when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end' 
when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 
when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 
when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 
when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end' 
when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 
when a.xtype =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' 
when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' 
when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
else '''NULL''' 
end as col,a.colid,a.name 
from syscolumns a where a.id = object_id(@tablename) 
and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36 
)t order by colid 
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename + ' where 1=1 and ' + isnull(@con,'') 
print @sqlstr 
exec( @sqlstr) 
set nocount off 
end

本文来自:http://www.q1010.com/179/7437-0.html

注:关于SQL Server 将表数据生成Insert脚本 比较好用的生成插入语句的SQL脚本的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:SQL SERVER

您可能感兴趣的文章

  • win2003 安装 sqlserver 2005的方法
  • SQL Server当恢复sqlserver bak文件时,原始的用户无法删除的解决方法
  • SQL Server分页存储过程(三)在sqlserver中打造更加准确的分页结果
  • 分页存储过程(二)在sqlserver中返回更加准确的分页结果
  • SQLServer上查看SQL语句的执行时间的方法
  • SQL Server 2000数据库同步 同步两个SQLServer数据库的内容
  • SQLServer触发器创建、删除、修改、查看示例代码
  • MMC提示不能打开文件SQLServerEnterpriseManager.MSC的解决方法
  • Sqlserver 存储过程中结合事务的代码
  • SQL SERVER 错误22022 SQLServerAgent当前未运行的解决方法
上一篇:SQL Server 的T-SQL的基本对象
下一篇:SQL Server 数据库被注入解决方案
热门文章
  • SQL Server SQL获取第一条记录的方法
  • SQL Server出现System.OutOfMemoryException异常的解决方法
  • SQL Server的 update from 语句的简单示例
  • SQL Server 数据库备份方法菜鸟教程
  • SQL Server 多表关联时在where语句中慎用trim()方法
  • SQL Server数据类型及长度限制详细说明
  • mybaits非配置原因,导致SqlSession was not registered for synchronization异常解析
  • SQL Server 收缩后对数据库的使用有影响吗?
  • SQL Server 格式导致的Excel导入sql出现异常的解决方法
  • SQL Server 连接服务器出现错误 7391的解决方法
  • 最新文章
    • SQL Server存储过程基本语法的简单示例
    • sql查询时增加自动编号和分页的简单示例
    • sql轻松应付百万数据的高效数据分页存储过程的简单示例
    • sql获取一条数据中所有字段的名称和值的实现方法
    • sql分割函数的简单示例
    • SQL Server异常捕获的简单示例
    • SQL SERVER回滚恢复误操作数据的实现方法
    • SQL Server函数或存储过程中抛出异常的实现方法
    • SQL Server创建数据库的完整代码
    • SQL Server创建数据库的命令用法示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。