[SQL] SQL将一个表转化成建表语句的存储过程 →→→→→进入此内容的聊天室

来自 , 2019-05-02, 写在 SQL, 查看 100 次.
URL http://www.code666.cn/view/f8d2e80c
  1. SET QUOTED_IDENTIFIER ON
  2. GO
  3. SET ANSI_NULLS ON
  4.  
  5. GO
  6.  
  7. CREATE proc pGetInsertSQL (@TableName VARCHAR(256))
  8. AS
  9. BEGIN
  10. SET nocount ON
  11. DECLARE @sqlstr VARCHAR(4000)
  12. DECLARE @sqlstr1 VARCHAR(4000)
  13. DECLARE @sqlstr2 VARCHAR(4000)
  14. SELECT @sqlstr='select ''insert '+@tablename
  15. SELECT @sqlstr1=''
  16. SELECT @sqlstr2=' ('
  17. SELECT @sqlstr1= ' values ( ''+'
  18. SELECT @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' FROM (SELECT CASE
  19. -- 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'
  20. WHEN a.xtype =104 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'
  21. WHEN a.xtype =175 THEN 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
  22. WHEN a.xtype =61 THEN 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
  23. 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'
  24. WHEN a.xtype =62 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
  25. WHEN a.xtype =56 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'
  26. WHEN a.xtype =60 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
  27. WHEN a.xtype =239 THEN 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
  28. 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'
  29. WHEN a.xtype =231 THEN 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
  30. WHEN a.xtype =59 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
  31. WHEN a.xtype =58 THEN 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
  32. WHEN a.xtype =52 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'
  33. WHEN a.xtype =122 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
  34. WHEN a.xtype =48 THEN 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'
  35. -- 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'
  36. WHEN a.xtype =167 THEN 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
  37. ELSE '''NULL'''
  38. END AS col,a.colid,a.name
  39. 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
  40. )t ORDER BY colid
  41.  
  42. SELECT @sqlstr=@sqlstr+LEFT(@sqlstr2,len(@sqlstr2)-1)+') '+LEFT(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename
  43. -- print @sqlstr
  44. EXEC( @sqlstr)
  45. SET nocount off
  46. END
  47.  
  48. GO
  49. SET QUOTED_IDENTIFIER OFF
  50. GO
  51. SET ANSI_NULLS ON
  52. GO
  53.  
  54.  
  55. CREATE TABLE test(name VARCHAR(20))
  56. INSERT test
  57. SELECT '1' UNION ALL
  58. SELECT '2' UNION ALL
  59. SELECT '3' UNION ALL
  60. SELECT '4'
  61. GO
  62.  
  63. EXEC pGetInsertSQL 'test'
  64.  
  65. -------------------------------------------------
  66. INSERT test (name) VALUES ( '1')
  67. INSERT test (name) VALUES ( '2')
  68. INSERT test (name) VALUES ( '3')
  69. INSERT test (name) VALUES ( '4')
  70. //SQL/1230

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

这儿你可以回复上面这条便签

captcha