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根据条件生成插入语句--单表的更多相关文章

  1. sql语句-单表查询

    一:单表查询 CREATE TABLE `Score`( `s_id` ), `c_id` ), `s_score` ), PRIMARY KEY(`s_id`,`c_id`) ); ); ); ); ...

  2. mysql——查询语句——单表查询——(概念)

    一.基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ...

  3. mysql中有条件的插入语句

    今天在参加笔试的过程中,看到一道题,大概意思就是说,当满足了条件就执行插入语句,当时就蒙了,之前从来都没有考虑过满足条件才插入的情况,所以一直都是这样写的 insert into table_name ...

  4. [SqlServer]如何向数据库插入带有单引号(')的字符串

    今天在做一个复制功能的时候,发现存在单引号字符串与INSERT INTO 语句的' '产生冲突. 在网络上找到了一个这样功能 如何向数据库插入带有单引号(')的字符串 用SQL语句往数据库某字段(字符 ...

  5. Mysql | 总结 | 常用的查询语句(单表查询)

    1. 查询单表全部 select* from 数据表名; 2. 查询单表中一个或者多个字段 select 字段1,字段2 from 数据表名; 3. 查询单表中的指定信息 select* from 数 ...

  6. Mysql 语句单表查询

    一基本查询 -- 创建商品表 CREATE TABLE products( pid INT PRIMARY KEY AUTO_INCREMENT, pname VARCHAR(20), price D ...

  7. mysql——查询语句——单表查询——(示例)

    一.基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ...

  8. sqlserver低版本生成插入脚本

    --将表数据生成SQL脚本的存储过程 CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar( ...

  9. 手撸Mysql原生语句--单表

    select from where group by having order by limit 上面的所有操作是有执行的优先级的顺序的,我们将执行的过程可以总结为下面所示的七个步骤. 1.找到表:f ...

随机推荐

  1. epoll中epoll_data_t 中fd和ptr的用法

    https://blog.csdn.net/u011123091/article/details/81867078 Linux高性能服务器P152

  2. Html checkbox全选

    html中全选 <table class="data-table td-center"> <tr> <td><input type=&qu ...

  3. 一些学习比较好的网站,及es6学习好网站,前端用的相关库,及知道的插件

    图形可视化:d3js  echarts https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects ...

  4. C++11并发编程:多线程std::thread

    一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改.现在在C++11中只需使用语 ...

  5. unity对敏感词库处理的记录

    string txt = Resources.Load<TextAsset>("minganci").ToString(); //string txt = " ...

  6. 编译impala2.0.0

    使用redhat5.8没编译成功,改用redhat6.4最终编译成功. 参考官方的文档https://github.com/cloudera/Impala/tree/v1.2.2 不知道官方的read ...

  7. a[i]==i[a]==*(i+a)==*(a+i)

    在C语言中,如果我们要访问一个数组的某个下标对应的元素,通常的写法是a[i].但从汇编的角度看,写成i[a]一点问题都没有. 下面通过代码给出证明. o foo1.c int main(int arg ...

  8. 九度oj 1464 Hello World for U 2012年浙江大学计算机及软件工程研究生机试真题

    题目1464:Hello World for U 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3872 解决:1082 题目描述: Given any string of N (> ...

  9. 公司管理系列--Facebook是如何营造企业文化的[转]

      本文讲下硅谷创业公司的文化,去过硅谷公司或者是看过硅谷公司报道的人,都会惊讶硅谷创业公司里面有如此奇特且活力十足的文化.在中国,企业文化是一个被滥用但是却又缺乏解读的概念,很多国内企业对保持公司的 ...

  10. C#之RabbitMQ系列(一)

    RabbitMQ–环境搭建 MQ MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接 ...