id  int
fieldname varchar(20)
sql varchar(4000)
kind varchar(20)这是一个表的字段,
其中sql里面保存的是sql句子
我现在想查找到符合条件的sql句子,(比如kind=5)
然后再执行每一个这个sql句子
要求写成存储过程

解决方案 »

  1.   


    declare @sql varchar(max)
    declare @i int
    declare @row int
    select *,px=row_number() over (order by getdate()) into #tb from tb
    select @row = max(px) from #tb
    set @i = 1
    while @i <= @row
    begin
    select @sql = [sql] from #tb where px = @i
    exec(@sql)
    set @i = @i + 1
    end
      

  2.   

    try
    create proc sp_test @kind varchar(20)
    as
    begin
      declare @sql varchar(8000)
      select @sql=isnull(@sql,'')+[sql] from tb
      exec (@sql)
    end
      

  3.   


    如果kind=5的记录不止一条,得像三楼的小三那样循环一下
      

  4.   


    create proc wming
    as
    begin
     declare @sql varchar(4000),@i int,@j int
     
     select identity(int,1,1) rn, [sql]
     into #t 
     from tab 
     where [sql] like '%kind=5%' select @i=1,@j=@@ROWCOUNT
     while(@i<=j)
     begin
       select @sql=[sql] from #t where rn=@i
       exec(@sql)
       select @i=@i+1
     end
     
    end
      

  5.   

    忘了一件事了,
    exec(@sql)
    之后把这个值插入另一个表中。
      

  6.   


    create proc wming
    as
    begin
     declare @sql varchar(4000),@i int,@j int
     
     select identity(int,1,1) rn, [sql]
     into #t 
     from tab 
     where [sql] like '%kind=5%' select @i=1,@j=@@ROWCOUNT
     while(@i<=j)
     begin
       select @sql=[sql] from #t where rn=@i
       exec(@sql)
       insert into [另一个表]([字段]) values(@sql)
       select @i=@i+1
     end
     
    end
      

  7.   

    数据库是里遍历好象是非CURSOR不行啊。
      

  8.   

    这个可能会出问题吧!sql字段里存储的是t-sql语句,能确定是查询 删除 更新 或 其他 功能的相应句子么。
      

  9.   

    create table s_ManagerSQL
    (
    id varchar(30)
    FieldName varchar(20) not null,
    Sql varchar(4000) not null,
    Kind varchar(20) not null,
    IsEnble bit not null,
    FunDes varchar(4000)
    )insert into s_ManagerSQL
    select '101','F01','select top 1 Currwidth from wprocess','1','1','宽度语句' union all
    select '102','测试','select sheetno,planno,jobkind from Jobsheet','2','1','宽度语句' union all
    select '103','F03','select top 5 jobsheetno,Qty,CurrQty,WorkQty from wprocess where prevQty<>0 and workQty<>0','3','1','宽度语句' union all
    select '104','F04','select top 1 Currweight from wprocess','1','1','宽度语句' union all
    select '105','F05','select top 1 CurrQty from wprocess','1','1','宽度语句'像这样的句子,然后执行每一个句子,插入到
    CREATE TABLE [twdyeing].[ManagerData](
    [CurrDate] [smalldatetime] NULL,
    [F01] [decimal](18, 4) NULL,
    [F02] [decimal](18, 4) NULL,
    [F03] [decimal](18, 4) NULL,
    [F04] [decimal](18, 4) NULL,
    [F05] [decimal](18, 4) NULL,
    [F06] [decimal](18, 4) NULL,
    [F07] [decimal](18, 4) NULL,
    [F08] [decimal](18, 4) NULL,
    [F09] [decimal](18, 4) NULL,
    [F10] [decimal](18, 4) NULL
    ) ON [PRIMARY]表中,
    执行的是kind为1的,这样的句子只反回一个值,kind为别的不执行,
    kind为1 的,第二个字段上面写着,插入到表中的那个字段,
    select '101','F01','select top 1 Currwidth from wprocess','1','1','宽度语句' union all
    比如这一枚执行完后插入到ManagerData的F01字段中
      

  10.   

    这个直接去前台拼接sql字段把插入的也写上去吧!如果按楼主这样子的数据处理不来的。