sqlserver根据条件生成插入语句--单表

CREATE 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 ‘[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 =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 @[email protected]+left(@sqlstr2,len(@sqlstr2)-1)+‘) ‘+left(@sqlstr1,len(@sqlstr1)-3)+‘)‘‘ from ‘[email protected] + ‘ where ‘[email protected]
-- print @sqlstr
exec(@sqlstr)
set nocount off
end

 执行 exec proc_insert ‘Jobs‘, ‘ JOBID=‘‘3D7ED3A4-F0FB-45D2-8AA1-79C96609B522‘‘‘

如果报错,可能是拷贝网页的时候有空格。我把 from 和 where 附近的空格去掉就成功了。

原地址

原文地址:https://www.cnblogs.com/cvol/p/9872153.html

时间: 2024-07-29 22:17:59

sqlserver根据条件生成插入语句--单表的相关文章

SQL Server根据表或视图批量生成插入语句并BCP导出成文本

--声明变量 DECLARE @TableName VARCHAR(100),@RunStr VARCHAR(MAX),@RunRC VARCHAR(MAX),@FinalRun VARCHAR(MAX) --在master库创建数据载体,也可以建立在其他数据库,但一定要是实体表,因为BCP导出不支持临时表 IF OBJECT_ID('master.dbo.tempinsertstr') IS NOT NULL DROP TABLE master.dbo.tempinsertstr CREATE

mysql中有条件的插入语句

今天在参加笔试的过程中,看到一道题,大概意思就是说,当满足了条件就执行插入语句,当时就蒙了,之前从来都没有考虑过满足条件才插入的情况,所以一直都是这样写的 insert into table_name(column...)values(value...) 这样的语句.所以那道题没有做出来. 今天就把插入语句好好复习一下. 第一种情况插入指定字段 insert into table_name(column1,column2)values(value1,value2); 第二种情况插入所有字段:前提

Mysql 语句单表查询

一基本查询 -- 创建商品表 CREATE TABLE products( pid INT PRIMARY KEY AUTO_INCREMENT, pname VARCHAR(20), price DOUBLE ); -          查询的格式: select [distinct]*(所有)|字段名,...字段名 from 表名 [where 条件过滤] -- 查询指定字段信息pname,price SELECT pname,price FROM products; -- 去除金额重复的记

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

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

Sqlserver 导出insert插入语句

SQLserver,确有些难度,自带的工具都无法完成这个操作.经过查证资料可以通过写一个存储过程来完成这个操作. 存储过程如下: SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO if exists(select 1 from sysobjects where id=object_id('BicashyOutputData') and xtype='P') drop procedure BicashyOutputData; GO create p

SQLServer学习笔记&lt;&gt;.基础知识,一些基本命令,单表查询(null top用法,with ties附加属性,over开窗函数),排名函数

Sqlserver基础知识 (1)创建数据库 创建数据库有两种方式,手动创建和编写sql脚本创建,在这里我采用脚本的方式创建一个名称为TSQLFundamentals2008的数据库.脚本如下:   同时往数据库表插入一些数据,用户后续对数据库的sql的练习.在这里有需要的可以下载相应的脚本进行数据库的初始化.我放到百度云上面,请戳 我:http://yun.baidu.com/share/link?shareid=3635107613&uk=2971209779,提供了<Sqlserver

oracle中复制表和数据 &amp;&amp; 多表插入语句

创建测试表和测试数据 create table test  (id number,name varchar(10));  insert into  test values(1,'liufang');  insert into  test values(2,'xiaozhang');  insert into  test values(3,'dawei');  insert into  test values(4,'laotan');  insert into  test values(5,'la

指定查询条件,查询对应的集合List(单表)

TestDao.java(测试类) @Test public void findCollectionByConditionNoPage(){  ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  IElecTextService elecTextService = (IElecTextService) ac.getBean(IElecTextService.SERVICE_NAME);