首页 > 数据库 >sqlserver表数据导出

sqlserver表数据导出

时间:2023-09-25 09:13:18浏览次数:33  
标签:varchar xtype when 导出 sqlserver end NULL 数据 name

declare @tablename varchar(max)
select @tablename=''

declare @sqlstr varchar(max)
declare @sqlstr1 varchar(max)
declare @sqlstr2 varchar(max)
select @sqlstr='select ''insert into '+@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)

 

标签:varchar,xtype,when,导出,sqlserver,end,NULL,数据,name
From: https://www.cnblogs.com/xesx/p/17727119.html

相关文章

  • PostgreSQL教程:单引号和双引号的使用、数据类型转换
    单引号和双引号在PGSQL中,写SQL语句时,单引号用来标识实际的值。双引号用来标识一个关键字,比如表名,字段名。--单引号写具体的值,双引号类似MySQL的``标记,用来填充关键字--下面的葡萄牙会报错,因为葡萄牙不是关键字select1.414,'卡塔尔',"葡萄牙";数据类型转换第一种方式:只需要在值......
  • PostgreSQL教程:数据类型
    PGSQL支持的类型特别丰富,大多数的类型和MySQL都有对应的关系名称说明对比MySQL布尔类型boolean,标准的布尔类型,只能存储true,falseMySQL中虽然没有对应的boolean,但是有替换的类型,数值的tinyint类型,和PGSQL的boolean都是占1个字节。整型smallint(2字节),integer(4字节),bigint(8字节)跟MySQL没......
  • PostgreSQL教程:SQL案例(创建用户、创建数据库、创建schema、赋权)
    需求构建一个用户构建一个数据库在这个数据库下构建一个schema(数据库默认有一个public的schema)将这个schema的权限赋予用户在这个schema下构建一个表将表的select,update,insert权限赋予用户实现--准备用户createuserlaozhengwithpassword'laozheng';--准备数据库createda......
  • Linux Centos7 虚拟环境安装Mysql数据库(超详细图文讲解)
    1、进入Centos7虚拟机,使用wget下载Mysql相应的rpm包下载:wgethttp://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm如果没有wget命令,可以使用yum安装,yuminstallwget2、执行rpm命令,安装rpmrpm-ivhmysql57-community-release-el7-8.noarch.rpm 3、安装Mys......
  • Java连接MSSQL2012数据报TLS10 is not accepted by client preferences [TLS13, TLS12
    这一问题好像是因为Java新版本禁用了些老的加密算法引起的,解决方法为修改java.security文件里的配置信息即可。我用的是Java21,在安装目录 Java\jdk-21\conf\security下找到java.security文件,用记事本打开,搜索TLSv1,大概在752行的位置有如下配置信息:jdk.tls.disabledAlgorithm......
  • SAP WM初阶 - 上架界面里的托盘化数据
    SAPWM初阶-上架界面里的托盘化数据在SAPWM模块里,LT06或者LT04都能产生转储单,用于收货后对于物料库存的上架或者下架后对于物料的下架。在上架界面里,会出现Palletization数据,如下所示:选中某个TR点击‘TOinForeground’按钮,进入如下界面,在Palletization部分,看到有2X500数据,这些......
  • R语言使用Metropolis-Hastings采样算法自适应贝叶斯估计与可视化|附代码数据
    原文链接:http://tecdat.cn/?p=19889原文出处:拓端数据部落公众号 最近我们被客户要求撰写关于Metropolis-Hastings采样的研究报告,包括一些图形和统计输出。如果您可以写出模型的似然函数,则 Metropolis-Hastings算法可以负责其余部分(即MCMC)。我写了r代码来简化对任意模型的后......
  • R语言逻辑回归、决策树、随机森林、神经网络预测患者心脏病数据混淆矩阵可视化
    全文链接:https://tecdat.cn/?p=33760原文出处:拓端数据部落公众号概述:众所周知,心脏疾病是目前全球最主要的死因。开发一个能够预测患者心脏疾病存在的计算系统将显著降低死亡率并大幅降低医疗保健成本。机器学习在全球许多领域中被广泛应用,尤其在医疗行业中越来越受欢迎。机器......
  • R语言中的block Gibbs吉布斯采样贝叶斯多元线性回归|附代码数据
    全文链接:http://tecdat.cn/?p=11617最近我们被客户要求撰写关于blockGibbs吉布斯采样的研究报告,包括一些图形和统计输出。在这篇文章中,我将对多元线性回归使用block的Gibbs采样,得出block的Gibbs采样所需的条件后验分布。然后,对采样器进行编码,并使用模拟数据对其进行测试 ( 点......
  • R语言Gibbs抽样的贝叶斯简单线性回归仿真分析|附代码数据
    全文下载链接:http://tecdat.cn/?p=4612最近我们被客户要求撰写关于贝叶斯简单线性回归的研究报告,包括一些图形和统计输出。贝叶斯分析的许多介绍都使用了相对简单的教学实例(例如,根据伯努利数据给出成功概率的推理)。虽然这很好地介绍了贝叶斯原理,但是这些原则的扩展并不是直截了......