将表数据生成Insert脚本

-- =============================================
-- Author: 明永成
-- Create date: 2016-01-03
-- Description: 将表数据生成Insert脚本
-- Demo : exec pCreateInsertScript. ‘BexmCodeType‘,‘dictypeid = 61‘
-- =============================================
create 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 ‘[email protected]
select @sqlstr1=‘‘
select @sqlstr2=‘(‘
select @sqlstr1=‘values (‘‘+‘
select @[email protected]+col+‘+‘‘,‘‘+‘ ,@[email protected]+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 @[email protected]+left(@sqlstr2,len(@sqlstr2)-1)+‘) ‘+left(@sqlstr1,len(@sqlstr1)-3)+‘)‘‘ from ‘[email protected] + ‘ where 1=1 and ‘ + isnull(@con,‘‘)
print @sqlstr
exec( @sqlstr)
set nocount off
end

时间: 2024-08-09 03:30:31

将表数据生成Insert脚本的相关文章

MS SQLServer表数据生成Insert语句

关键词:SQLServer.表数据.生成Insert语句 反馈意见请联系:[email protected] 简介 数据库数据生成insert(MSSQL版),可将表中的数据生成insert或者update的sql脚本.比如您维护两个数据库,其中一个数据库中增加的数据也希望能够在另外一个数据中进行执行. 下载地址 下载地址:http://www.vidarsoft.cn/download/SQLInsert.zip Csdn下载地址:http://download.csdn.net/detail

根据MySQL表数据导出INSERT INTO语句的方法

因项目中有需要需根据MySQL表数据生成INSERT INTO语句,在网上找了些现成的代码,原作者是谁就不知道了,但是发现有BUG,不能适用,遂对他人代码进行了修改.修改后能较好的导出INSERT INTO语句.代码如下: package dwz.interaction; import java.io.*; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * Update by internetr

SQL Server里面导出SQL脚本(表数据的insert语句)

转载自:http://hi.baidu.com/pigarmy/blog/item/109894c445eab0a28326ac5a.html 最近需要导出一个表的数据并生成insert语句,发现SQL Server的自带工具并米有此功能.BAIDU一下得到如下方法(亲测OK) 用这个存储过程可以实现: CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar(1000) declare @c

备份数据表为insert 脚本

unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB, Datasnap.DBClient; type TForm1 = class(TForm) cds: TClientDataSet

SQL Server复制表结构和表数据生成新表的语句

参考:http://topic.csdn.net/t/20020621/09/820025.html SELECT   *   INTO   newTableName   FROM   oldTableName 此方法将把旧表的结构和数据同时copy生成新表,不过主键外键约束没有生成,需要手动设置.

数据生成python脚本

下面是项目结构: --data 存放数据的文件夹 --main.cpp 题目的正确数据 --prodData.py 生成输入输出数据脚本 --test.cpp 测试代码 --testData.py 测试代码是否正确脚本 prodData.py #-*- coding:utf-8 -*- ''' @auther: Starry @file: prodData.py @time: 2018/1/17 15:47 ''' import random import os DATASIZE = 10 ''

sqlserver低版本生成插入脚本

--将表数据生成SQL脚本的存储过程 CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar(1000) declare @columndata varchar(1000) declare @sql varchar(4000) declare @xtype tinyint declare @name sysname declare @objectId int declare @objectn

将表里的数据批量生成INSERT语句的存储过程 继续增强版

文章继续 桦仔兄的文章 将表里的数据批量生成INSERT语句的存储过程 增强版 继续增强... 本来打算将该内容回复于桦仔兄的文章的下面的,但是不知为何博客园就是不让提交!.... 所以在这里贴出来吧,算作继续增加文章中解决的:根据查询条件自动生成插入脚本的需求,其实这种需求还是蛮常见的. 本文着重解决了文中的脚本的schema问题,给调整了下,现在脚本能自动识别出不同的schema下同名的表的语句 修改后脚本如下: -- Author: <桦仔> -- Blog: <http://ww

将表里的数据批量生成INSERT语句的存储过程 增强版

原文:将表里的数据批量生成INSERT语句的存储过程 增强版 将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的库中 目前SQL Server里面是没有相关的工具根据查询条件来生成INSERT语句的,只有借助第三方工具(third party tools) 这种脚本网上也有很多,但是网上的脚本还是欠缺一些规范和功能,例如:我只想导出特定查询条件的数据,网上的脚本都是导出全表数据 如果表很大,对性能会有很大影响