//用游标遍历表tab_jl来创建子表
            string sqltext2="declare @id  int; declare pcurr cursor for select JL_ID from tab_jl;";            //定义变量id和游标pcurr,
            sqltext2+="open pcurr; fetch next from pcurr into @id ;";                                          //这段程序中有两张表,其中tab_jl是总表,我的想法是根据JL_ID遍历tab_jl
            sqltext2+="while (@@fetch_status = 0)";                                                               //如果tab_jl中GY_GYWF(是子表的表名)在数据库中没有,就create一张
            sqltext2+="begin;";  
            sqltext2+="if not exists(Select name from sysobjects where name=(Select tab_jl.JL_GYWF from tab_jl where tab_jl.JL_ID=id))";                               //还有这一步有没有错?
            sqltext2+="create table tab_jl.JL_GYWF(T int not null identity,GY_DWBZ bit,GY_FXMS varchar(50);";
            sqltext2+="fetch next from pcurr into @id;";  
            sqltext2+="end;";  
            sqltext2+="close pcurr;";
            sqltext2 += "deallocate pcurr;";
应该有不少问题,望指正。

解决方案 »

  1.   

                string sqltext2="declare @id  int; declare pcurr cursor for select JL_ID from tab_jl;";            //定义变量id和游标pcurr,
                sqltext2+="open pcurr; fetch next from pcurr into @id ;";                                          //这段程序中有两张表,其中tab_jl是总表,我的想法是根据JL_ID遍历tab_jl
                sqltext2+="while (@@fetch_status = 0)";                                                               //如果tab_jl中GY_GYWF(是子表的表名)在数据库中没有,就create一张
                sqltext2+="begin";  
                sqltext2+="if not exists(Select name from sysobjects where name=(Select tab_jl.JL_GYWF from tab_jl where tab_jl.JL_ID=@id))";                               //还有这一步有没有错?
                sqltext2+="create table [tab_jl.JL_GYWF](T int not null identity,GY_DWBZ bit,GY_FXMS varchar(50));";
                sqltext2+="fetch next from pcurr into @id;";  
                sqltext2+="end";  
                sqltext2+="close pcurr;";
                sqltext2 += "deallocate pcurr;";
      

  2.   

    我这一段是C#中的一段代码,begin和end后的';'去掉后,报错"begin if   end close"附近有语法错误
      

  3.   

    fetch next from pcurr into @id ;    还有这一句可以怎么理解啊?
      

  4.   

    create table [tab_jl.JL_GYWF](T int not null identity,GY_DWBZ bit,GY_FXMS varchar(50);这一句好像有问题,提示tab_jl不具有访问权限,怎么改啊?
      

  5.   

    你這樣自己組合地SQL是不是要加上點空格看看。。
    sqltext2+=" begin ";  
    sqltext2+=" end ";