`
bailangfei3344
  • 浏览: 42159 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

MSSQL 表数据生成 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
 
分享到:
评论

相关推荐

    MS SQL数据导出Insert语句(SQLDMO)

    1、将表、视图及SQL语句查询结果生成Insert语句 2、将选定数据库对象(表、视图、存储过程等)生成Script 3、支持单表的SQL语句,关联表以第一个表为准 4、丰富了生成选项配置 5、树型结构方便查询操作 6、保存连接及...

    SQL Server中存储过程比直接运行SQL语句慢的原因

    当对数据库进行复杂操作时(如对多个表进行 Update,Insert,Query,Delete时),可将此复杂操作用存储过程封装起来与数据库提供的事务处理结合一起使用。可以极大的提高数据 库的使用效率,减少程序的执行时间,这一点...

    代码自动生成工具(vb类 java类 sql存储过程...)

    MSSQL 表操作存储过程的示例 (update, insert, delete) 支持数据字典生成 生成格式可以是 Word 或Excel 格式   支持简单的 SQL 语句操作 请从http://progame.vip.myrice.com/下载最新版本

    MySQL与MSSQl使用While语句循环生成测试数据的代码

    25 begin INSERT INTO demotable (id,item1,item2) VALUES (@a,”abc”,”123″) set @a = @a + 1 end MySQL中,使用while循环处理数据方法:需要新建为存储过程,直接调用执行存储过程。 示例代码: 代码如下: ...

    经典全面的SQL语句大全

    这一操作可以通过好几种方式来完成,但是 SELECT 语句只显示一种可能(这里的ID 是自动生成的号码): SQL = "SELECT * FROM Customers WHERE ID BETWEEN " & RNumber & " AND " & RNumber & "+ 9"  注意:以上...

    SQL Server 中 EXEC 与 SP_EXECUTESQL 的区别.doc

    MSSQL为我们提供了两种动态执行SQL语句的命令,分别是 EXEC 和 SP_EXECUTESQL ,我们先来看一下两种方式的用法。 先建立一个表,并添加一些数据来进行演示: 复制代码 CREATE TABLE t_student( Id INT NOT NULL, ...

    asp.net知识库

    将数据库表中的数据生成Insert脚本的存储过程!!! 2分法-通用存储过程分页(top max模式)版本(性能相对之前的not in版本极大提高) 分页存储过程:排序反转分页法 优化后的通用分页存储过程 sql语句 一些Select检索高级...

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part1

    实例244 将文本文件中数据存储到数据库中 316 4.5 文件的压缩与解压 318 实例245 PHP中压缩RAR文件 319 实例246 PHP中将上传文件转换成RAR文件 320 实例247 PHP中对RAR文件进行解压 321 实例248 PHP中压缩ZIP文件 ...

    PHP开发实战1200例(第1卷).(清华出版.潘凯华.刘中华).part2

    实例244 将文本文件中数据存储到数据库中 316 4.5 文件的压缩与解压 318 实例245 PHP中压缩RAR文件 319 实例246 PHP中将上传文件转换成RAR文件 320 实例247 PHP中对RAR文件进行解压 321 实例248 PHP中压缩ZIP文件 ...

    mssqldump:用于为 MS SQL 创建 dbdump 的 Powershell 实用程序,类似于 mysqldump 和 pg_dump

    这包含模式、函数和视图的 SQL 脚本以及所有数据的INSERT语句。 生成的脚本将尝试删除任何同名的现有数据库,因此如果目标服务器上不存在此数据库,您将收到一条错误消息。 ##以下是您可以使用它做什么的一些示例...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    日期类型 date 7字节 用于存储表中的日期和时间数据,取值范围是公元前4712年1月1日至公元9999年12月31日,7个字节分别表示世纪、年、月、日、时、分和秒 二进制数据类型 row 1~2000字节 可变长二进制数据,在具体...

    迷你SQL2000

    +在查看我的表中增加导出表结构、索引、视图、存储过程、用户函数为SQL语句的功能。 1.2.8 ^允许修改密码处使用空密码,以方便一些要求空密码的软件连接; *修正win10下查询分析器缺少动态库和语法高亮未激活的...

Global site tag (gtag.js) - Google Analytics