SQL将一个表转化成建表语句的存储过程

SQL将一个表转化成建表语句的存储过程,第1张

概述SQL将一个表转化成建表语句的存储过程

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

SET QUOTED_IDENTIFIER ONGOSET ANSI_NulLS ONGOCREATE proc pGetInsertsql (@tablename varchar(256))asbeginset nocount ondeclare @sqlstr varchar(4000)declare @sqlstr1 varchar(4000)declare @sqlstr2 varchar(4000)select @sqlstr='select ''insert '[email protected]select @sqlstr1=''select @sqlstr2=' ('select @sqlstr1= ' values ( ''+'select @@L_419_1@+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 =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),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.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),2)'+' end'when a.xtype =58 then 'case when '+a.name+' is null then ''NulL'' else '+'''''''''+'+'convert(varchar(23),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.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.namefrom syscolumns a where a.ID = object_ID(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36)t order by colIDselect @[email protected]+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '[email protected]-- print @sqlstrexec( @sqlstr)set nocount offendGOSET QUOTED_IDENTIFIER OFFGOSET ANSI_NulLS ONGOcreate table test(name varchar(20))insert testselect '1' union allselect '2' union allselect '3' union allselect '4'goexec pGetInsertsql 'test'-------------------------------------------------insert test (name) values ( '1')insert test (name) values ( '2')insert test (name) values ( '3')insert test (name) values ( '4')

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的SQL将一个表转化成建表语句的存储过程全部内容,希望文章能够帮你解决SQL将一个表转化成建表语句的存储过程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1165263.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存