sqlserver根据条件生成插入语句--单表
ALTER proc [dbo].[proc_insert] (@tablename varchar(256),@where varchar(max))
as
begin
set nocount on
declare @sqlstr varchar(MAX)
declare @sqlstr1 varchar(MAX)
declare @sqlstr2 varchar(MAX)
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 =36 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+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
AND COLUMNPROPERTY(a.id, a.name, 'IsIdentity') <> 1
)t order by colid
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename + ' where '+@where
-- print @sqlstr
exec(@sqlstr)
set nocount off
end
执行 exec proc_insert 'Jobs', ' JOBID=''3D7ED3A4-F0FB-45D2-8AA1-79C96609B522'''
如果报错,可能是拷贝网页的时候有空格。我把 from 和 where 附近的空格去掉就成功了。
xtype参照
xtype=34 'image'
xtype= 35 'text'
xtype=36 'uniqueidentifier'
xtype=48 'tinyint'
xtype=52 'smallint'
xtype=56 'int'
xtype=58 'smalldatetime'
xtype=59 'real'
xtype=60 'money'
xtype=61 'datetime'
xtype=62 'float'
xtype=98 'sql_variant'
xtype=99 'ntext'
xtype=104 'bit'
xtype=106 'decimal'
xtype=108 'numeric'
xtype=122 'smallmoney'
xtype=127 'bigint'
xtype=165 'varbinary'
xtype=167 'varchar'
xtype=173 'binary'
xtype=175 'char'
xtype=189 'timestamp'
xtype=231 'nvarchar'
xtype=239 'nchar'
xtype=241 'xml'
xtype=231 'sysname'
sqlserver根据条件生成插入语句--单表的更多相关文章
- sql语句-单表查询
一:单表查询 CREATE TABLE `Score`( `s_id` ), `c_id` ), `s_score` ), PRIMARY KEY(`s_id`,`c_id`) ); ); ); ); ...
- mysql——查询语句——单表查询——(概念)
一.基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ...
- mysql中有条件的插入语句
今天在参加笔试的过程中,看到一道题,大概意思就是说,当满足了条件就执行插入语句,当时就蒙了,之前从来都没有考虑过满足条件才插入的情况,所以一直都是这样写的 insert into table_name ...
- [SqlServer]如何向数据库插入带有单引号(')的字符串
今天在做一个复制功能的时候,发现存在单引号字符串与INSERT INTO 语句的' '产生冲突. 在网络上找到了一个这样功能 如何向数据库插入带有单引号(')的字符串 用SQL语句往数据库某字段(字符 ...
- Mysql | 总结 | 常用的查询语句(单表查询)
1. 查询单表全部 select* from 数据表名; 2. 查询单表中一个或者多个字段 select 字段1,字段2 from 数据表名; 3. 查询单表中的指定信息 select* from 数 ...
- Mysql 语句单表查询
一基本查询 -- 创建商品表 CREATE TABLE products( pid INT PRIMARY KEY AUTO_INCREMENT, pname VARCHAR(20), price D ...
- mysql——查询语句——单表查询——(示例)
一.基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ...
- sqlserver低版本生成插入脚本
--将表数据生成SQL脚本的存储过程 CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar( ...
- 手撸Mysql原生语句--单表
select from where group by having order by limit 上面的所有操作是有执行的优先级的顺序的,我们将执行的过程可以总结为下面所示的七个步骤. 1.找到表:f ...
随机推荐
- CommandBehavior.CloseConnection有何作用
其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...
- mysql故障总结
MYSQL故障排查 https://zhuanlan.zhihu.com/p/27834293
- Jmeter调试脚本之断言
前言: jmeter中有个元件叫做断言(Assertion),它的作用和loadrunner中的检查点类似: 用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. ...
- 2-3 树/红黑树(red-black tree)
2-3 tree 2-3树节点: null节点,null节点到根节点的距离都是相同的,所以2-3数是平衡树 2叉节点,有两个分树,节点中有一个元素,左树元素更小,右树元素节点更大 3叉节点,有三个子树 ...
- hdu 3794 Magic Coupon
浙大计算机研究生保研复试上机考试-2011年 贪心: 注意:输入输出用scanf printf 可以加快速度,用cin WA #include<iostream> #include&l ...
- FocusBI: 总线矩阵(原创)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...
- Gradient Optimization
Gradient Optimization Gradient Descent Batch Gradient Descent Mini-Batch Gradient Descent Stochastic ...
- Codeforces 156 A——Message——————【思维题】
A. Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- DIY了一下自己blog的UI
当年才学前端时就想改自己blog的UI,然鹅当时没看见那个“申请JS权限”,一直以为blog不能随意DIY样式,只改了少许CSS.现在重新看看设置管理选项,简单修改了一下样式(注意:修改样式之前发邮件 ...
- [android] 切换界面的通用处理
实现不改变activity,只切换View 抽取View界面的基类 利用面向对象多态的思路,实现通用 TitleManager.java 管理标题 package com.tsh.lottery.vi ...