SYBASE中生成所有建表语句的过程

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                 --经常在用,感觉还不错。在数据移植的时候,配上BCP,那可是非常的方便
  1. if exists(select 1 from sysobjects where name = 'sp_gent' and type = 'P')  
  2.   drop procedure sp_gent  
  3. go  
  4.   
  5. create procedure sp_gent    
  6. @tblname varchar(30) = null,    
  7. @prechar varchar(4) = null,   --$:no print    
  8. @table_dll varchar(16384) = null out,    
  9. @dbname varchar(32) = null,    
  10. @droptg char(1) = '1',    
  11. @prxytx varchar(255) = null,    
  12. @replace varchar(20) = null,    
  13. @tabtype varchar(1) = 'A'--A:所有表;P:代理表;U:用户表    
  14. @indextg varchar(3) = 'TPI',  --T:纯表;P:主键;I:纯索引;J:除主键外的纯索引(和TP使用与I相同,和I同时使用I失效)    
  15. @table_seg varchar(32) = null,    
  16. @index_seg varchar(32) = null    
  17. as    
  18. begin    
  19.      set nocount on     
  20.     
  21.     if @tblname is null begin    
  22.         declare @c_tblname varchar(30)    
  23.         declare cur_1 cursor for    
  24.         select name from sysobjects where type = 'U' order by name    
  25.         open cur_1    
  26.         fetch cur_1 into @c_tblname    
  27.         while @@sqlstatus = 0 begin    
  28.             exec sp_gent     
  29.                 @tblname = @c_tblname,    
  30.                 @prechar = @prechar,    
  31.                 @dbname  = @dbname ,    
  32.                 @droptg  = @droptg ,    
  33.                 @prxytx  =  @prxytx ,    
  34.                 @replace = @replace,    
  35.                 @tabtype = @tabtype, --A:所有表;P:代理表;U:用户表    
  36.                 @indextg = @indextg, --A:表和索引;T:纯表;I:纯索引    
  37.                 @table_seg = @table_seg,    
  38.                 @index_seg = @index_seg    
  39.             fetch cur_1 into @c_tblname    
  40.         end    
  41.         close cur_1    
  42.         deallocate cursor cur_1    
  43.         return    
  44.     end    
  45.     
  46.     declare @obj_id int    
  47.     declare @sysstat2 int    
  48.     declare @username varchar(30)    
  49.     
  50.      select @obj_id = id, @sysstat2 = sysstat2 ,@username  = user_name(uid)    
  51.         from sysobjects where name = @tblname and type = 'U'    
  52.     if @@rowcount <> 1    
  53.     begin    
  54.         print 'table %1! not exists', @tblname    
  55.         goto err    
  56.     end    
  57.      if @sysstat2 & 1024 = 1024 begin    
  58.         if upper(@tabtype) in ('U')    
  59.             goto ok    
  60.     end    
  61.     else begin    
  62.         if upper(@tabtype) in ('P')    
  63.             goto ok    
  64.     end    
  65.     
  66.     declare @colname varchar(30)        --列名    
  67.     declare @typename varchar(30)       --类型名称    
  68.     declare @usertype smallint          --类型ID    
  69.     declare @length int                 --长度    
  70.     declare @prec tinyint               --有效位数    
  71.     declare @scale tinyint              --精度    
  72.     declare @def_id int             --默认值id    
  73.     declare @nulls tinyint              --空值    
  74.     declare @ident tinyint              --标识列    
  75.     declare @index_dll varchar(16384)    
  76.     
  77.     declare @def_text varchar(100)    
  78.     declare @ide_text varchar(30)    
  79.     declare @nul_text varchar(30)    
  80.     
  81.     declare @cns_text varchar(500)    
  82.     declare @uni_pri varchar(40), @non_clu varchar(40), @non_uni varchar(40)    
  83.     
  84.     declare @lock_scheme varchar(100)    
  85.     
  86.     declare @keys varchar(500), @i int    
  87.     declare @thiskey varchar(30)    
  88.     declare @sorder char(4)    
  89.     select @keys = "", @i = 1    
  90.     
  91.     declare @cns_name varchar(30), @status int, @indid int    
  92.     declare @idx_name varchar(50)    
  93.     
  94.     declare @CRNW varchar(2)    --回车换行    
  95.     declare @TAB char(1)    
  96.     
  97.     select @CRNW = convert(varchar(2), 0x0d0a)    
  98.     select @TAB = convert(char(1), 0x09)    
  99.     
  100.     declare @dbname_dot varchar(35)    
  101.     if ltrim(@dbname) is null    
  102.         select @dbname = null,@dbname_dot = null    
  103.     else    
  104.         select @dbname = @dbname + '.',@dbname_dot = @dbname + '..'    
  105.     
  106.     declare @table_name varchar(30)    
  107.     select @table_name = case when ltrim(@replaceis not null then @replace else @tblname end    
  108.     
  109.     declare @prefix_table varchar(2)    
  110.     select @prefix_table = case when ltrim(@prxytx) is not null then 'r_' else null end    
  111.     
  112.     if charindex('T',@indextg) > 0 begin    
  113.         if @droptg <> '0'    
  114.             select @table_dll = "if exists(select 1 from "+@dbname_dot    
  115.             +"sysobjects where name = '"+@prefix_table    
  116.             +@table_name+"' and type = 'U')"    
  117.             +@CRNW+@TAB+'drop table '+@dbname+@username + '.'    
  118.             +@prefix_table    
  119.             +@table_name+@CRNW    
  120.             +case when @sysstat2 & 1024 = 1024    
  121.                      then @TAB+'exec sp_dropobjectdef '+@table_name+@CRNW    
  122.                 when ltrim(@prxytx) is not null    
  123.                     then @TAB+'exec sp_dropobjectdef r_'+@table_name+@CRNW    
  124.                 else null     
  125.             end    
  126.             +'go'+@CRNW    
  127.         else    
  128.             select @table_dll = null    
  129.         
  130.         if @sysstat2 & 1024 = 1024 begin    
  131.             declare @OS_file varchar(255)    
  132.             select @OS_file = char_value from sysattributes    
  133.                     where class = 9 and attribute = 1 and    
  134.                     object_cinfo = @tblname    
  135.             if @@rowcount = 0 begin    
  136.                 print '取代理表前缀失败%1!', @tblname    
  137.                 goto err    
  138.             end    
  139.             select @table_dll = @table_dll+"exec sp_addobjectdef "    
  140.             +@table_name    
  141.             +", '"+@OS_file+"', 'table'"+@CRNW+    
  142.             "create existing table " + @dbname+@username + "."    
  143.             +@table_name + " ("    
  144.         end    
  145.         else if ltrim(@prxytx) is not null    
  146.              select @table_dll = @table_dll+"exec sp_addobjectdef r_"    
  147.             +@table_name+", '"+@prxytx    
  148.             +@table_name+"', 'table'"+@CRNW    
  149.             +"create existing table " + @dbname+@username + ".r_"    
  150.             +@table_name + " ("    
  151.          else    
  152.             select @table_dll = @table_dll+'create table ' + @dbname+@username + '.'    
  153.             +@table_name + ' ('    
  154.         
  155.         --如果在sybsystemprocs数据库下提交,以下注释掉    
  156.             
  157.         declare @tablna varchar(255)    
  158.          --select @tablna = tablna from knp_tabl where tablcd = @tblname    
  159.         --if @@rowcount = 0    
  160.             select @tablna = null    
  161.         if ltrim(@tablna) is not null    
  162.             select @table_dll = @table_dll + '    --'+@tablna    
  163.         
  164.         select @prechar = case when @prechar is not null then left(@prechar+space(4),4) else @prechar end    
  165.         if @prechar <> '$' begin    
  166.             if @prechar is not null begin    
  167.                 declare @temp_dll varchar(16384),@print_dll varchar(16384)    
  168.                  select @temp_dll = @table_dll    
  169.                 select @temp_dll = @prechar + @temp_dll    
  170.                 while charindex(@CRNW,@temp_dll) > 0 and char_length(@temp_dll) <> charindex(@CRNW,@temp_dll)+1 begin    
  171.                     select @print_dll = @print_dll + left(@temp_dll,charindex(@CRNW,@temp_dll) - 1) + @CRNW+@prechar    
  172.                     select @temp_dll = substring(@temp_dll,charindex(@CRNW,@temp_dll)+char_length(@CRNW),char_length(@temp_dll))    
  173.                 end    
  174.                  select @print_dll = @print_dll + @temp_dll    
  175.                 print '%1!',@print_dll    
  176.             end    
  177.             else    
  178.                 print '%1!',@table_dll    
  179.         end    
  180.             
  181.         select @table_dll = @table_dll + @CRNW    
  182.         
  183.          if ltrim(@table_seg) is null begin    
  184.             select @table_seg = s.name    
  185.                 from sysobjects o, syssegments s, sysindexes i    
  186.                     where o.id = object_id(@tblname)    
  187.                         and i.id = o.id    
  188.                          and i.indid < 2    
  189.                         and i.segment = s.segment    
  190.             if @@rowcount = 0 begin     
  191.                 print '表%1!所在的段不存在',@tblname    
  192.                 goto err    
  193.             end    
  194.         end    
  195.     end    
  196.     
  197.     --确定表是否有完整性约束    
  198.     declare @have_con char(1)    
  199.     if exists (select 1 from sysindexes where id = @obj_id and status2 & 2 = 2 )    
  200.         and (ltrim(@prxytx) is null or @sysstat2 & 1024 = 1024)    
  201.         select @have_con = '1'    
  202.     else    
  203.          select @have_con = '0'    
  204.     
  205.     
  206.     if charindex('T',@indextg) > 0 begin    
  207.         declare @col_int int    
  208.         select @col_int = count(*) from syscolumns    
  209.             where id = @obj_id    
  210.         
  211.         declare cur_col cursor for    
  212.             select b.name, b.usertype, c.name , b.length, b.prec, b.scale, b.cdefault,    
  213.                     convert(bit,b.status&8) as Nulls,    
  214.                     convert(bit,b.status&128) as Ident    
  215.                 from sysobjects a, syscolumns b, systypes c    
  216.                  where a.name = @tblname and a.type = 'U'    
  217.                     and  a.id = b.id    
  218.                     and b.usertype = c.usertype    
  219.                 order by b.colid    
  220.         
  221.         open cur_col    
  222.         fetch cur_col into @colname, @usertype, @typename, @length, @prec, @scale, @def_id, @nulls, @ident    
  223.         while @@sqlstatus = 0    
  224.         begin    
  225.             --系统定义的数据类型    
  226.             if  @usertype < 100    
  227.             begin    
  228.         
  229.                 if rtrim(@typename) in ('char','varchar','nchar','nvarchar')    
  230.                     select @typename = @typename + '('convert(varchar,@length) +')'    
  231.                 else if @typename in ('numeric','decimal')    
  232.                     select @typename = @typename + '(' + convert(varchar,@prec) +  ',' + convert(varchar,@scale) + ')'    
  233.                 else if @typename in ('float','double')    
  234.                     select @typename = @typename + '(' + convert(varchar,@prec) + ')'    
  235.                 else if @typename in ('binary','varbinary')    
  236.                      select @typename =  @typename + '(' + convert(varchar,@length) + ')'    
  237.             end    
  238.         
  239.             select @ide_text = case @ident when 1 then 'identity' else null end    
  240.             select @nul_text = case @nulls when 1 then '    null'  else 'not null' end    
  241.         
  242.             if @def_id > 0    
  243.             begin    
  244.                 select @def_text = ltrim(rtrim(b.text))    
  245.                     from sysobjects a, syscomments b    
  246.                         where a.id = @def_id and a.id = b.id    
  247.                  if @@rowcount <> 1    
  248.                 begin    
  249.                     print '取default失败%1!', @def_id    
  250.                     goto err    
  251.                 end    
  252.                 while charindex(@TAB,@def_text) > 0    
  253.                     select @def_text = stuff(@def_text,charindex(@TAB,@def_text),char_length(@TAB),' ')    
  254.                 while charindex('  ',@def_text) > 0    
  255.                     select @def_text = stuff(@def_text,charindex('  ',@def_text),char_length('  '),' ')    
  256.                 select @def_text = rtrim(ltrim(@def_text))    
  257.         
  258.             end    
  259.             else    
  260.                 select @def_text = null    
  261.         
  262.             declare @thiscol varchar(500)    
  263.             select @thiscol =    
  264.                     case when char_length(@colname) <= 10 then left(@colname+space(10),10) else @colname end     
  265.                     + ' ' + case when char_length(@typename) <= 15 then left(@typename+space(15),15) else @typename end    
  266.                     + ' ' + @def_text    
  267.                     + ' ' + @ide_text    
  268.                     + ' ' + @nul_text    
  269.         
  270.             if @i = @col_int and (@have_con = '0' or charindex('P',@indextg) <= 0)    
  271.                 select @thiscol  = @thiscol +  '  '    
  272.             else    
  273.                 select @thiscol  = @thiscol + ' ,'    
  274.         
  275.             --如果在sybsystemprocs数据库下提交,以下注释掉    
  276.             declare @colmna varchar(255)    
  277.             select @colmna = null    
  278.             --select @colmna = colmna from knp_colm where tablcd = @tblname and colmcd = @colname    
  279.         
  280.             if ltrim(@colmna) is not null    
  281.                 select @thiscol = @thiscol + '    --'+@colmna    
  282.         
  283.             if @prechar <> '$'    
  284.                 print '%1!%2!',@prechar, @thiscol    
  285.         
  286.             select @table_dll = @table_dll + @thiscol + @CRNW    
  287.         
  288.             select @i = @i + 1    
  289.             fetch cur_col into @colname, @usertype, @typename, @length, @prec, @scale, @def_id, @nulls, @ident    
  290.         end    
  291.     end    
  292.     
  293.     if @have_con = '1' and charindex('P',@indextg) > 0    
  294.     begin    
  295.     
  296.         select @cns_name = name, @status = status, @indid = indid    
  297.             from sysindexes where id = @obj_id and status2 & 2 = 2    
  298.     
  299.         --print 'exist constraint... status = %1!', @status    
  300.     
  301.         if @indid = 1     
  302.             select @non_clu = 'clustered'    
  303.         else if @indid > 1    
  304.         begin    
  305.             if  @status & 16 = 16    
  306.                 select @non_clu = 'clustered'    
  307.             else    
  308.                 select @non_clu = 'nonclustered'    
  309.         end    
  310.     
  311.         if @status & 2048 = 2048    
  312.             select @uni_pri = 'primary key'    
  313.         else    
  314.             select @uni_pri = 'unique'    
  315.     
  316.         select @cns_text = 'constraint ' + @cns_name + ' ' + @uni_pri + ' ' + @non_clu    
  317.     
  318.         select   @i = 1, @keys = ''    
  319.         select @thiskey = index_col(@tblname, @indid, @i)    
  320.         while @thiskey <> null    
  321.         begin    
  322.             if @i > 1    
  323.             begin    
  324.                 select @keys = @keys + ", "    
  325.             end    
  326.     
  327.             if ltrim(@keys) is null    
  328.                 select @keys = @thiskey    
  329.             else    
  330.                 select @keys = @keys + @thiskey    
  331.     
  332.             select @sorder = index_colorder(@tblname, @indid, @i)    
  333.             if (@sorder = "DESC")    
  334.                  select @keys = @keys + " " + @sorder    
  335.     
  336.             select @i = @i + 1    
  337.             select @thiskey = index_col(@tblname, @indid, @i)    
  338.         end    
  339.     
  340.         select @cns_text = @cns_text + ' (' + @keys + ')'    
  341.     
  342.         if ltrim(@table_seg) is null begin    
  343.             select @table_seg = s.name    
  344.                 from sysobjects o, syssegments s, sysindexes i    
  345.                     where o.id = object_id(@tblname)    
  346.                         and i.id = o.id    
  347.                         and i.indid < 2     
  348.                         and i.segment = s.segment    
  349.             if @@rowcount = 0 begin    
  350.                 print '表%1!所在的段不存在',@tblname    
  351.                 goto err    
  352.             end    
  353.         end    
  354.     
  355.         if charindex('T',@indextg) <= 0    
  356.              select @cns_text = 'alter table '+@dbname+@username + '.'+@table_name+' add '+@cns_text+ " on '" + @table_seg + "'"    
  357.         if @prechar <> '$'    
  358.             print '%1!%2!',@prechar,@cns_text    
  359.     
  360.         select @table_dll = @table_dll + @cns_text    
  361.     
  362.     end    
  363.     
  364.     if charindex('T',@indextg) > 0 begin    
  365.         if @prechar <> '$'    
  366.             print '%1!%2!',@prechar, ') '    
  367.         
  368.         select @table_dll = left(@table_dll,char_length(@table_dll)-1) +  @CRNW + ')'    
  369.         
  370.         --表锁计划    
  371.         if @sysstat2 & 8192 = 8192    
  372.             select @lock_scheme = 'lock allpages'    
  373.         else if @sysstat2 & 16384 =  16384    
  374.             select @lock_scheme = 'lock datapages'    
  375.         else if @sysstat2 & 32768 = 32768    
  376.             select @lock_scheme = 'lock datarows'    
  377.         select @table_dll = @table_dll + @CRNW + @lock_scheme    
  378.         
  379.         if @prechar <> '$'    
  380.             print '%1!%2!',@prechar, @lock_scheme    
  381.         
  382.         select @table_seg = "on '"+ @table_seg+"'"    
  383.         select @table_dll = @table_dll + @CRNW + @table_seg + @CRNW+'go'+@CRNW    
  384.         
  385.         if @prechar <> '$' begin    
  386.             print '%1!%2!',@prechar, @table_seg    
  387.             print '%1!go',@prechar    
  388.         end    
  389.     end    
  390.     
  391.     if ltrim(@prxytx)  is not null or @sysstat2 & 1024 = 1024    
  392.         goto ok    
  393.     
  394.     if charindex('T',@indextg) > 0 begin    
  395.         declare @part_num int,@partition varchar(255)    
  396.         select @part_num = count(*)    
  397.             from syspartitions    
  398.             where id = object_id(@tblname)    
  399.         if @part_num <> 0 begin    
  400.             select @partition = 'alter table '+ @username + '.' + @table_name + ' partition '+convert(varchar,@part_num)    
  401.             select @table_dll = @table_dll + @CRNW + @partition    
  402.             if @prechar <> '$'    
  403.                 print '%1!%2!',@prechar, @partition    
  404.         end    
  405.     end    
  406.     
  407.     --select @table_dll as table_dll    
  408.     -------------------------------------------------------------------------------------    
  409.     --检查其他索引    
  410.     declare @idx_seg  varchar(32)    
  411.     if charindex('I',@indextg) > 0 or charindex('J',@indextg) > 0 begin    
  412.         if exists  (select 1 from sysindexes where id = @obj_id and indid <> 0 and     
  413.             (status2 & 2 <> 2 or charindex('P',@indextg) <= 0 and charindex('J',@indextg) <= 0))    
  414.     
  415.         begin    
  416.             declare cur_idx cursor for    
  417.                 select name, indid, status from sysindexes    
  418.                     where id = @obj_id and indid <> 0 and     
  419.             (status2 & 2 <> 2 or charindex('P',@indextg) <= 0 and charindex('J',@indextg) <= 0)    
  420. --                  (status2 & 2 <> 2 or charindex('P',@indextg) <= 0)    
  421.             open cur_idx    
  422.             fetch cur_idx into @idx_name, @indid, @status    
  423.             while @@sqlstatus = 0     
  424.             begin    
  425.         
  426.                 if @indid = 1    
  427.                     select @non_clu = 'clustered'    
  428.                 else if @indid > 1    
  429.                 begin    
  430.                     if  @status & 16 = 16    
  431.                         select @non_clu = 'clustered'    
  432.                     else    
  433.                         select @non_clu = 'nonclustered'    
  434.                 end    
  435.         
  436.                 if @status & 2 = 2    
  437.                     select @non_uni = 'unique '    
  438.                 else    
  439.                      select @non_uni = null    
  440.         
  441.                 select @i = 1,@keys = ''    
  442.                 select @thiskey = index_col(@tblname, @indid, @i)    
  443.                 while @thiskey <> null    
  444.                 begin    
  445.                     if @i > 1    
  446.                       begin    
  447.                         select @keys = @keys + ", "    
  448.                     end    
  449.         
  450.                     if ltrim(@keys) is null    
  451.                         select @keys = @thiskey    
  452.                     else    
  453.                          select @keys = @keys + @thiskey    
  454.         
  455.                     select @sorder = index_colorder(@tblname, @indid, @i)  

    给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
    这里写图片描述
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值