`
jiataibin
  • 浏览: 41592 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

SQL SERVER:把表里的数据导出成为INSERT INTO脚本的存储过程

阅读更多
declare @objectId int
set @objectId=object_id('proc_insert')
if @objectId is not null
begin
    drop proc proc_insert
end
go

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
go
执行方法(将tablename替换为相应的数据表名):
exec proc_insert 'tablename'
分享到:
评论
1 楼 252831020 2010-12-14  
哈哈。。非常不错!

相关推荐

    sqlserver表数据生成insertsql脚本(导出成insertsql脚本).pdf

    SQL Server是一种强大的关系数据库管理系统,提供了多种方式来生成INSERT SQL脚本,以便将数据导出到其他数据库或系统中。在本文中,我们将介绍如何使用SQL Server 2008中文版生成INSERT SQL脚本。 生成INSERT SQL...

    Sql Server导出数据 INSERT 脚本

    可以将数据库中的数据导出insert into脚本, sql server2008以上版本已经自带导出数据库数据成insert脚本功能,该工具针对2005以下版本.使用时先选择数据库服务器地址,在右侧选择Sql Server login,输入数据库账号...

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

    在SQL Server数据库管理中,有时我们需要将表中的数据导出为Insert语句,这在数据迁移、备份或测试环境中非常常见。"SQL Server表数据导出成Insert语句的工具"是一个专门为此目的设计的应用程序,它能帮助数据库管理...

    SQLServer导出带数据的脚本

    SQL Server 导出带数据的脚本 SQL Server 是一个功能强大的关系数据库管理系统,它提供了多种方式来导出数据库结构和数据。今天,我们将探讨如何使用 SQL Server Management Studio (SSMS) 导出带数据的脚本。 一...

    从SQLSERVER数据库中导出记录生成insert into 语句

    从SQLSERVER数据库中导出记录生成insert into 语句

    SQLServer数据库表中数据导出成SQL语句工具

    总之,“SQLServer数据库表中数据导出成SQL语句工具”是一种方便的解决方案,它简化了数据库表数据的导出过程,便于在不同环境间进行数据迁移和备份。了解并熟练掌握这类工具的使用,对于SQL Server管理员和开发人员...

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

    综上所述,存储过程`dbo.UspOutputData`提供了一种灵活且强大的方法,用于将MS SQL Server中的数据导出为SQL插入语句,极大地简化了数据迁移和脚本化工作流程。然而,使用时也应充分考虑其局限性和潜在风险,采取...

    数据导出为Insert 脚本

    在MS SQL SERVER中,导出数据为Insert脚本通常是为了备份数据、迁移数据或者在测试环境中重现生产环境的数据状态。这个过程可以通过SQL Server Management Studio (SSMS) 来实现: 1. 打开SSMS,连接到你的SQL ...

    DB2数据库自动导出数据INSERT SQL脚本工具

    总之,"DB2数据库自动导出数据INSERT SQL脚本工具"是DB2数据库管理的得力助手,通过自动化生成INSERT语句,简化了数据导入导出的过程,为数据库的日常维护提供了便利。使用这个工具,可以让你更加专注于业务逻辑和...

    SQL Server将数据导出SQL脚本的方法

    总的来说,SQL Server中将数据导出为SQL脚本涉及到对数据库的理解、T-SQL的掌握以及可能需要借助存储过程或第三方工具。熟练掌握这些技巧,对于管理和维护SQL Server数据库至关重要。记得在操作过程中注意数据安全,...

    SQL Server导出表到EXCEL文件的存储过程

    - **描述内容**:“SQL Server导出表到EXCEL文件的存储过程,整理sql如何把表导出” - **描述解读**:这段描述进一步明确了文章的主题,即通过一个具体的存储过程来实现从SQL Server数据库中的表数据导出至Excel文件...

    将sqlserver表中指定数据转化为insert语句

    在SQL Server中,有时我们需要将现有表中的特定数据导出为INSERT语句,以便在其他数据库或备份中重建这些记录。这通常在迁移数据、创建测试数据或者进行数据备份时非常有用。以下是如何将SQL Server表中指定数据转化...

    SQL SERVER 将select数据生成insert语句

    它是一个实用的辅助程序,能够自动生成`INSERT INTO`语句,帮助用户将数据从一个表导出为可执行的SQL脚本。这个工具可能包含以下功能: 1. **选择表**:用户可以选择要导出数据的特定表。 2. **指定列**:允许用户...

    sqlserver 导出表及字段说明脚本.rar

    2. **导出表结构**:在SQL Server中,可以使用`sp_help`系统存储过程或`information_schema`视图来获取表的结构信息,包括表名、字段名、数据类型、是否为主键等。`sp_help 'tableName'`将显示关于特定表的所有信息...

    SQL SERVER 表数据导出脚本

    根据提供的文件信息,我们可以深入分析并提取出与“SQL Server 表数据导出脚本”相关的几个关键知识点。 ### 知识点一:SQL Server 存储过程的创建与删除 在给定的部分内容中,首先出现的是一个存储过程的创建逻辑...

    SQL Server数据表记录导出Insert形式(源码)

    SQL Server的导入导出功能可以导出创建数据库各对象的脚本,却不提供导出目标表的现有数据为Insert语句的功能,网上也有此类存储过程,但要运行还得给客户数据库上create它,昨天兴致突来,写了一个工具ExpertSQL,...

    SQL Server导出数据字典工具

    SQL Server是一款广泛应用...总的来说,"SQL Server导出数据字典工具"是数据库管理员和开发人员的得力助手,它简化了获取和管理SQL Server数据库元数据的过程,提高了工作效率,同时也为数据库的维护和优化提供了便利。

    C#对SQLServer中的数据导入导出.pdf

    ### C#对SQL Server中的数据导入导出:深入解析与实践 #### 一、引言 在数据库管理系统(DBMS)的应用中,特别是在企业级应用领域,数据的导入与导出是一项常见且至关重要的任务。这不仅涉及到数据的迁移、备份,...

    SQL SERVER自动导出Word数据库文档

    6. **PowerShell**:另一种方法是使用PowerShell,它提供了丰富的SQL Server模块,可以调用SQL命令行工具(如sqlcmd或bcp)来导出数据,然后通过PowerShell脚本控制Word文档的操作。 7. **Third-party Tools**:...

Global site tag (gtag.js) - Google Analytics