首页 > 数据库 >sql server 将数据库表里面的数据,转为insert语句,方便小批量转移数据

sql server 将数据库表里面的数据,转为insert语句,方便小批量转移数据

时间:2024-08-17 14:26:23浏览次数:8  
标签:case insert varchar name when server sql end xtype

create proc [dbo].[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 =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+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

 先在数据库创建以上存储过程,然后执行如下语句:

exec proc_insert 'MyTableName'

就会得到一批insert 语句,复制到其他地方去执行就可以了,有时候id是identity的不允许显示指定的话,像下面这样子执行

set identity_insert 表名 ON--打开
 
insert语句...
insert语句...

 
set identity_insert 表名 OFF--关闭

  

 

标签:case,insert,varchar,name,when,server,sql,end,xtype
From: https://www.cnblogs.com/Jerseyblog/p/18364352

相关文章

  • 【SQL】连续登录
    目录题目分析代码题目Table: Activity+--------------+---------+|ColumnName|Type|+--------------+---------+|player_id|int||device_id|int||event_date|date||games_played|int|+--------------+-------......
  • MySQL数据库基础
    目录1.数据库的操作1.1  创建数据库1.2查看数据库1.3选中数据库1.4删除数据库2.常用数据类型2.1数值类型2.2字符串类型2.3日期类型3.表的操作3.1创建表3.2查看所有表3.3查看表的结构3.4删除表4.内容重点总结5.练习1.数据库的操作1.1  ......
  • MySQL数据库入门,pycharm连接数据库—详细讲解
    一.安装MySQL1.常用MySQL5.7,首先安装MySQL,(一) (二)(三)(四)(五)2.配置环境变量打开MySQL安装路径,在其中找到bin文件,复制路径,打开系统高级设置,点击环境变量,在下方找到path,将bin文件路径粘贴其中。3.检验安装是否完成win+r 输入cmd,打开命令提示符,输入指令:netstart|......
  • Ubuntu安装mysql 以及远程连接mysql Windows—适合初学者的讲解(详细)
    目录准备工作一.Xshell中操作(1)在虚拟机中安装mysql(2)连接Windows数据库(3)进入linux数据库。(4)修改mysql配置文件二.Windows命令窗口操作 需要软件虚拟机,Xshell。准备工作1.首先需要打开虚拟机,然后在终端上安装ssh,远程传输协议。安装指令为:sudo apt install......
  • 【漏洞复现】某骋BPM Handler SQL注入漏洞
               声明:本文档或演示材料仅用于教育和教学目的。如果任何个人或组织利用本文档中的信息进行非法活动,将与本文档的作者或发布者无关。一、漏洞描述某骋BPM系统是一款全面的商业流程管理平台,旨在帮助企业自动化和优化业务流程。它集成了工作流引擎、表单......
  • 坑!火山引擎云数据库 MySQL 版节点内存只增不减
    火山引擎云数据库说明文档什么是云数据库MySQL版?云数据库MySQL版是火山引擎基于开源数据库MySQL打造的弹性、可靠的在线关系型数据库服务。MySQL实例使用云原生方式部署,结合本地SSD存储类型,提供高性能读写能力;完全兼容MySQL引擎,并提供实例管理、备份恢复、日志管理......
  • Mysq-造数工具mysql_random_data_load
    1.背景实际开发中,做sql优化,为确保可行性,可能需要模拟出大量数据验证。此处,介绍一个造数工具mysql_random_data_load,体验了下,很方便。2.使用2.1下载下载:mysql_random_data_load(releases页面)选择合适的版本,例如。2.2安装上传到服务器直接解压即可。tar-zxvfmysql_r......
  • 浅看MySQL数据库
    有这么一句话:“一个不会数据库的程序员不是合格的程序员”。有点夸张,但是确是如此。透彻学习数据库是要学习好多知识,需要学的东西也是偏难的。我们今天来看数据库MySQL的一些简单基础东西,跟着小编一起来看一下吧。什么是数据库数据库(Database)是按照数据结构来组织、存储和管......
  • [20240815]oracle21c环境变量ORACLE_PATH与SQLPATH(windows).txt
    [20240815]oracle21c环境变量ORACLE_PATH与SQLPATH(windows).txt--//我记忆以前测试过这个问题,当时是家里的笔记本,安装oracle12.2cforwindows.OS:windows7,发现无法访问SQLPATH或者--//ORACLE_PATH环境变量定义的路径下login.sql文件.我当时解决办法就是登录手工执行init.sq......
  • [20240816]oracle21c环境变量ORACLE_PATH与SQLPATH(linux).txt
    [20240816]oracle21c环境变量ORACLE_PATH与SQLPATH(linux).txt--//我记忆以前测试过这个问题,当时是家里的笔记本,安装oracle12.2cforwindows.OS:windows7,发现无法访问SQLPATH或者--//ORACLE_PATH环境变量定义的路径下login.sql文件.我当时解决办法就是登录手工执行init.sql......