`
ritaleo
  • 浏览: 102526 次
  • 来自: ...
社区版块
存档分类
最新评论

MS SqlServer 生成 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 @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)  
  set nocount off  
 end  
分享到:
评论

相关推荐

    导出SQL数据为Insert语句

    3. **第三方工具**:除了官方工具和自编脚本外,还有一些第三方软件如SqlDbx、Red Gate SQL Compare等能帮助你快速生成Insert语句。 在导出过程中,必须考虑几个关键因素: - **数据类型**:确保Insert语句中的值与...

    MS SQL Server7概述

    用户可以通过SQL Server Analysis Services(SSAS)进行数据建模和报表生成。 ### 8. 数据导入导出 SQL Server 7 提供了数据导入导出工具,方便从其他数据源(如文本文件、Excel等)导入数据,或者将数据导出到...

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

    当前支持oracle、sybase、mysql和ms sqlServer数据库。 你可以修改参数文件、增加新的jdbc驱动来增加新数据库种类的支持。 使用方法: 软件为免安装的绿色软件。系统必须已经安装1.4版本以上的java虚拟机。 解...

    SQL语句生成器 V2.0

    综上所述,SQL语句生成器 V2.0是MS SQL Server用户的得力助手,它通过自动化生成SQL语句,降低了数据库管理的复杂性,提升了工作效率。然而,考虑到它对特定数据库系统的依赖,用户在使用前需确保系统兼容性,以充分...

    让GUNS 5.0支持MS SQL

    - 将MySQL中的`INSERT INTO`语句转换为MS SQL Server的语法: ```java // 原始的MySQL语句 String sql = "INSERT INTO `guns`.`sys_menu`(...)"; // 修改后的MS SQL Server语句 sql = "INSERT INTO dbo.sys_...

    Delphi中sql语句的使用总结

    在不同的数据库系统中(如Access或MS SQL Server),日期的表示方式可能会有所不同。因此,在构建SQL查询时需要特别注意日期格式。 - **对于Access**:可以使用`#`符号来包围日期,如: ```delphi adoquery1.SQL...

    MS sql-server培训

    学习者需要掌握基本的SQL语句,如SELECT用于查询数据,INSERT用于插入新数据,UPDATE用于更新现有数据,DELETE用于删除数据,以及DDL(Data Definition Language)用于创建和修改表结构。 2. **T-SQL扩展**:T-SQL...

    MSsql数据表生成sql脚本

    MS SQL Server提供了内置的功能来生成数据库对象的脚本。在SQL Server Management Studio (SSMS) 中,你可以右键点击数据库或具体的数据表,选择“任务” -&gt; “生成脚本”,然后按照向导设置脚本的选项,如是否包含...

    Log Explorer for SQL Server v4.22 含注册机

    他可以支持SQLServer7.0、SQLServer2000和SQLServer2005,提取标准数据库的日志文件或者备份文件中的信息。 其中提供两个强大的工具:日志分析浏览,对象恢复。具体功能如下: l 日志文件浏览 l 数据库变更审查 l...

    把msSqlServer 中的数据导出成 insert into语句

    将Microsoft SQL Server(简称MS SQL Server)中的数据导出为SQL插入语句是一个常见的数据库管理操作,主要用于数据备份、迁移或脚本化目的。在给定的文件信息中,一个存储过程`dbo.UspOutputData`被设计用于实现这...

    数据导出为Insert 脚本

    本文将详细讲解如何在MS SQL SERVER环境下,将数据库中的表数据导出为Insert语句,以及相关的重要知识点。 首先,Insert语句是SQL语言中用于向表格中插入新数据的基本命令。它的基本格式如下: ```sql INSERT INTO...

    SQL批量录入数据到数据库中

    在上述的`批量录入数据.sql`文件中,很可能包含了这样的BULK INSERT语句,用于将数据从文本文件导入到SQL Server的表中。在实际操作时,需要确保文件路径正确,且文件中的数据格式与BULK INSERT语句的配置相匹配。 ...

    C#图片转换成二进制流并且保存到sql server数据库.pdf

    在上面的代码中,我们首先创建了SqlConnection对象,然后使用SqlCommand对象来执行INSERT语句。我们将二进制流保存到SqlParameter对象,然后将其添加到SqlCommand对象中。最后,我们执行SQL语句将二进制流保存到SQL ...

    将Excel生成SQL语句完成数据库导入.doc

    在某些情况下,我们需要将Excel中的数据导入到像MS SQL Server、MySQL或Oracle这样的关系型数据库中。本文将介绍一种方法,通过生成SQL插入语句来实现这一过程。 首先,数据库管理系统通常提供数据导入工具,允许...

    vc 使用ado 连接使用sql server 2000数据库

    对于插入操作,先创建一个命令对象,然后设置SQL INSERT语句,最后执行: ```cpp BSTR insertSql = _bstr_t(L"INSERT INTO TableName (FieldName1, FieldName2) VALUES (?, ?)"); pCommand.CreateInstance(__uuidof...

    SQLServer2000开发者指南(pdf)

    ### SQL Server 2000 开发者指南关键知识点概览 #### 第1章 Transact-SQL DDL - **1.1 创建数据库** - 数据库是存储数据的容器,Transact-SQL 提供了 `CREATE DATABASE` 命令来创建新的数据库。 - 创建数据库时...

    SQL_import_export_tool.rar

    4. **SQL脚本生成**:对于结构和数据较少的数据库,可以手动编写`CREATE TABLE`和`INSERT INTO`语句来实现迁移。 5. **第三方工具**:市场上有许多第三方工具,如Red Gate的SQL Compare和SQL Data Compare,它们...

    MS SQL 存储过程解密

    在MS SQL Server中,存储过程是一种预编译的SQL语句集合,用于执行特定的任务。为了保护存储过程的逻辑或敏感数据,管理员可以选择将其加密。加密后的存储过程代码无法直接查看,只能通过特殊的方法进行解密。 ####...

Global site tag (gtag.js) - Google Analytics