我有一数据表!
table1
里面有多条记录
其中有一字段v1
v1里面保存的是字段计算指令我想实现打开数据表table1
然后循环读取v1
执行v1的内容select * from table1 
while ....
@v1=select v1 from table
exec(@v1)
.....完整的sql应该怎么写

解决方案 »

  1.   


    declare @v1 varchar(2000)
    declare curtable1 cursor  for 
    select v1
    from table1
    open curtable1
    fetch next from curtable1 into @v1
    while @@fetch_status = 0
    begin
    exec(@v1)
    fetch next from curtable1 into @v1
    end
    close curtable1
    deallocate curtable1
      

  2.   


    declare @v1 varchar(2000)
    declare curtable1 cursor  for 
    select v1
    from table1
    open curtable1
    fetch next from curtable1 into @v1
    while @@fetch_status = 0
    begin
    exec(@v1)
    fetch next from curtable1 into @v1
    end
    close curtable1
    deallocate curtable1
      

  3.   

    用表变量DECLARE @Table TABLE(ID INT IDENTITY(1,1),V1 VARCHAR(100))
    DECLARE @Line INT
    DECLARE @Total INT
    DECLARE @V1 VARCHAR(100)
    INSERT INTO @Table
    SELECT * FROM TABLE1SELECT @Total = MAX(Id)
    FROM @TableWHILE @Line <= @Total
    BEGIN
    SELECT @V1 = V1
    FROM @Table
    WHERE ID = @Line

    EXEC (@V1)

    SET @Line = @Line + 1

    END