首页 > 数据库 >MsSql 根据表名和条件,生成Insert语句

MsSql 根据表名和条件,生成Insert语句

时间:2023-04-15 22:32:47浏览次数:41  
标签:Insert varchar xtype when else 表名 end MsSql name

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 '表名','age=18'

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'

标签:Insert,varchar,xtype,when,else,表名,end,MsSql,name
From: https://blog.51cto.com/u_15949341/6192486

相关文章

  • mysql insert|replace语法
    insert语法replace语法版权声明:本文所有权归作者!商业用途转载请联系作者授权!非商业用途转载,请标明本文链接及出处!赞成、反驳、不解的小伙伴,欢迎一起交流!......
  • pretter 报 error Insert `·` prettier/prettier
    <c-switch:switchList="['自取','外送']"@change="switchChange"></c-switch>原因是//switchList里面的数组逗号后面要有一个空格<c-switch:switchList="['自取','外送']"@change="switchChange&quo......
  • Insert a scratch project into a ppt (MSPowerPoinT file)在powerpoint中播放Scratch
    Insertascratchprojectintoappt(MSPowerPoinTfile)在powerpoint中播放Scratch动画Contributedbyliupeng,March01,20120Comments4BookmarksAsupersimplewaytoinsertasbtoappt,asfollows:超级简单的实现Scratch的sb文件在ppt中播放,具体......
  • SearchInsert
    packageBisectionMethod;/***35.搜索插入位置*给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。*请必须使用时间复杂度为O(logn)的算法。***/publicclassSearchInsert{publi......
  • SQL-根据已有表结构创建表、修改表名
    stringsql_Rename_MainTable=string.Empty;//改表名(主表->历史表)的sqlstringsql_Create_MainTable=string.Empty;//通过历史表的表结构创建主表的sqlswitch(dbType){caseDbType.MySql:sql_Rename_MainTable="altertable'{0}'renameto'......
  • Sqlsugar哪个方法是实现了ADO.NET-MSSQL的SqlDataAdapter
    一、ADO.NET-MSSQL的SqlDataAdapter/*使用DataSet数据集更新数据*为了将数据库的数据填充到dataset中,则必须先使用adapter对象的方法实现填充*......
  • SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    Insert是T-sql中常用语句,InsertINTOtable(field1,field2,...)values(value1,value2,...)这种形式的在应用程序开发中必不可少。但我们在开发、测试过程中,经常会遇到需要......
  • MSSQL PIVOT和UNPIVOT使用说明
    一、版本要求1.数据库的最低版本要求为SQLServer2005或更高。2.必须将数据库的兼容级别设置为90或更高。3.查看我的数据库版本及兼容级别。如果不知道怎么看数据库......
  • 添加删除修改(insert,update,【delete和truncate】)
                    ......
  • 仅当指定列列表,且SET IDENTITY_INSERT为ON时,才能对自增列赋值
    #情景今天在做达梦数据库(DM7)的适配工作,发现了如上错误,#原因主要原因就是达梦数据库和oracle比较类似,你目前设置了主键自增,然后呢,现在还想手动设置主键,这咋能行呢#解......