2023年7月13日发(作者:)
MsSql根据表名和条件,⽣成Insert语句ALTER proc [dbo].[proc_insert] (@tablename varchar(256),@where varchar(max))asbeginset nocount ondeclare @sqlstr varchar(MAX)declare @sqlstr1 varchar(MAX)declare @sqlstr2 varchar(MAX)select @sqlstr='select ''INSERT '+@tablenameselect @sqlstr1=''select @sqlstr2=' ('select @sqlstr1= ' VALUES ( ''+'select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case
-- when =173 then 'case when '++' is null then ''NULL'' else'+'convert(varchar('+convert(varchar(4),*2+2)+'),'+ +')'+' end'when =36 then 'case when '++' is null then ''NULL'' else '+'''''''''+'+'replace('++','''''''','''''''''''')' + '+'''''''''+' end'when =127 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(20),'+ +')'+' end'when =104 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(1),'+ +')'+' end'when =175 then 'case when '++' is null then ''NULL'' else '+'''''''''+'+'replace('++','''''''','''''''''''')' + '+'''''''''+' end'when =61 then 'case when '++' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+ +',121)'+'+'''''''''+' end'when =106 then 'case when '++' is null then ''NULL'' else'+'convert(varchar('+convert(varchar(4),+2)+'),'+ +')'+' end'when =62 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(23),'+ +',2)'+' end'when =56 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(11),'+ +')'+' end'when =60 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(22),'+ +')'+' end'when =239 then 'case when '++' is null then ''NULL'' else '+'''''''''+'+'replace('++','''''''','''''''''''')' + '+'''''''''+' end'when =108 then 'case when '++' is null then ''NULL'' else'+'convert(varchar('+convert(varchar(4),+2)+'),'+ +')'+' end'when =231 then 'case when '++' is null then ''NULL'' else '+'''''''''+'+'replace('++','''''''','''''''''''')' + '+'''''''''+' end'when =59 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(23),'+ +',2)'+' end'when =58 then 'case when '++' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+ +',121)'+'+'''''''''+' end'when =52 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(12),'+ +')'+' end'when =122 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(22),'+ +')'+' end'when =48 then 'case when '++' is null then ''NULL'' else '+'convert(varchar(6),'+ +')'+' end'-- when =165 then 'case when '++' is null then ''NULL'' else'+'convert(varchar('+convert(varchar(4),*2+2)+'),'+ +')'+' end'when =167 then 'case when '++' is null then ''NULL'' else '+'''''''''+'+'replace('++','''''''','''''''''''')' + '+'''''''''+' end'else '''NULL'''end as col,,om syscolumns awhere = object_id(@tablename) and <>189 and <>34 and <>35 -- and <>36
AND COLUMNPROPERTY(, , 'IsIdentity') <> 1)t order by colidselect @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename + ' where'+@where-- print @sqlstrexec(@sqlstr)set nocount offend执⾏⽰例: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'
发布者:admin,转转请注明出处:http://www.yc00.com/web/1689244040a225528.html
评论列表(0条)