`
a881127b
  • 浏览: 64432 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

MS-SQL数据转inser语句

阅读更多
今天写程序时不小心删了这段时间做程序很重要的一个表中的一些数据,本来紧张死了(要是找不回来来了,工期又要拖后了),我在网上找了种方法,可以把数据库表中的数据转成sql insert语句的存储过程,特此和大家分享:
create proc proc_insert (@tablename varchar(256))  
as  
begin  
set nocount on  
declare @sqlstr varchar(4000)  
declare @sqlstr1 varchar(4000)  
declare @sqlstr2 varchar(4000)
select 'SET IDENTITY_INSERT '+@tablename +' ON'  
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 =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  
-- print @sqlstr  
exec( @sqlstr)  
select 'SET IDENTITY_INSERT '+@tablename +' OFF'
set nocount off  
end  
go  


使用如下例:
execute  proc_insert ActiveDirectory;
分享到:
评论

相关推荐

    MS SQL数据导出Insert语句

    1、将数据库中的表记录导出为Insert语句 2、支持选择数据库中的表,并排序生成 3、支持单表的SQL语句,关联表以第一个表为准 4、保存为.txt或.sql文档 5、对上次的操作会进行保存配置 欢迎提供宝贵意见^_^

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

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

    数据库表数据转为insert sql语句

    Data row to insert sql是一个小的工具软件,可以将数据库表中指定数据转换成相应的insert sql语句。目前支持的数据库类型为oracle,db2,ms sql server。 目前还有一些缺陷,还有待完善,具体如下: 1、对ms sql ...

    MS+SQL表数据导出成Insert语句的工具

    很好用,MS+SQL表数据导出成Insert语句的工具

    把任意数据库导出为其它数据库的insert(可执行程序+java代码)

    此软件用于把数据库里的数据导出为指定数据库类型的insert语句,用于数据备份或迁移到另一个数据库, 大文本和二进制字段被忽略。 当前支持oracle、sybase、mysql和ms sqlServer数据库。 你可以修改参数文件、增加新...

    SQL语句大全大全(官方修正典藏版)

    《SQL语句大全大全(官方修正典藏版)》包含MS SQL数据库全部语句及解释,十分齐全,都是按分类写的,也方便查找。 --数据操作 SELECT --从数据库表中检索数据行和列 INSERT --向数据库表添加新数据行 DELETE --从...

    表内容产生insert语句

    将ms-sql-server 指定的表数据,自动转换成相应的insert sql 语句,做数据迁移是比较方便. 说明: 1、3级执行,即将执行的结果再执行/再执行得到所要的 SQL语句; 2、不限表字段多少限制,网络上有个类似的...

    将MS SQL数据库表里的数据内容生成SQL执行语句

    将MS SQL数据库表里的数据内容生成SQL执行语句 生成如下语句: insert d_user (username,salt,password,mobile,creattime,lasttime,create_date,last_date,avatar,memo,actor,email,logins,threads,posts) values ( ...

    SQL.rar_SQL INSERT_drop_insert into_oracle_sybase

    SQL语句通常用于完成一些数据库的操作任务,比如在数据库中更新数据,或者从数据库中检索数据。使用SQL的常见关系数据库管理系统有:Oracle、 Sybase、 Microsoft SQL Server、 Access等等。虽然绝大多数的数据库...

    C#写的 SQL Builders

    .net项目中开发中有时会用到动态构建SQL语句,根据本人使用SQL的经验,写出了一些常用的SQL语句构建类,使得SQL的生成更自然。比起用字符串拼接不但好用而且不易出错。 如: InsertBuilder insertBuilder = new ...

    SQL语句生成及分析器(中文绿色)

    1、支持绝大部分数据库,包括 大型数据库Oracle,Sybase(包括SQL AnyWhere),DB2,MS_SQL 中型数据库MS_Access,MySQL 桌面型数据库Paradox,DBF系列数据库... 10.4 简单SQL查询语句转换为Delete,Update,Insert语句

    sql语句生成与分析器.rar

    11.4 简单SQL查询语句转换为Delete,Update,Insert语句 11.5 复制为字符串(支持对Java、C#、Delphi、VB、PowerBuilder开发语言的支持) 11.6 灵活的拖放功能 11.7 在线版本更新 11.8 查询结果输出为SQL脚本...

    SqlDbx Pers

    SqlDbx 是简单易用的数据库设备,SQL... 可以看到库中有多少表、多少存储过程、多少触发器、多少视图、多少函数,可以直接看到每个表的数据行数,可以导出查询结果的insert语句,还可以对比不同库中同一个表的差异等等

    SQL21日自学通

    第17 天使用SQL 来生成SQL 语句351 目标351 使用SQL 来生成SQL 语句的目的351 几个SQL*PLUS 命令352 SET ECHO ON/OFF353 SET FEEDBACK ON/OFF353 SET HEADING ON/OFF 353 SPOOL FILENAME/OFF353 START FILENAME354 ...

    SQL语句生成器 V2.0

    用于MS SQLSERVER 的sql语句生成器,可以生成create,insert等sql语句 注意只支持sqlserver 2000下测试过,其他没试过

    SQLServer2005考试题及答案

    选择题: 1、在MS SQL Server中,用来显示数据库信息的系统存储过程是( ) A sp_ dbhelp B sp_ db C sp_ help D sp_ helpdb 2、SQL语言中,删除一个表的命令是( ) A DELETE B DROP C CLEAR D REMORE 3、关系...

    LECCO SQL Expert (智能自动SQL优化)

    目前在商用数据库领域LECCO TechnologyLimited(灵高公司)拥有该技术并提供使用该技术的自动优化产品——LECCO SQL Expert,其支持Oracle、Sybase、MS SQLServer和IBMDB2数据库平台。该产品针对数据库应用的开发和...

    程序员的SQL金典4-8

     3.1.2 简化的INSERT语句  3.1.3 非空约束对数据插入的影响  3.1.4 主键对数据插入的影响  3.1.5 外键对数据插入的影响  3.2 数据的更新  3.2.1 简单的数据更新  3.2.2 带WHERE子句的UPDATE语句  3.2.3 非空...

    SQL 是用于访问和处理数据库的标准的计算机语言 在本教程中,您将学到如何使用 SQL 访问和处理数据系统中的数据,这类数据库

    SQL 可与数据库程序协同工作,比如 MS Access、DB2、Informix、MS SQL Server、Oracle、 Sybase 以及其他数据库系统。 不幸地是,存在着很多不同版本的 SQL 语言,但是为了与 ANSI 标准相兼容,它们必须以相似的方式...

    程序员的SQL金典.rar

     3.1.1 简单的INSERT语句  3.1.2 简化的INSERT语句  3.1.3 非空约束对数据插入的影响  3.1.4 主键对数据插入的影响  3.1.5 外键对数据插入的影响  3.2 数据的更新  3.2.1 简单的数据更新  3.2.2 带WHERE子句...

Global site tag (gtag.js) - Google Analytics