游标到不可怕,主要是是否可以传进字符串,作为sql语句或者作为条件部分

解决方案 »

  1.   

    可以!--测试数据
    select *
    into city
    from
    (
    select '1100,1200,1301' city
    union
    select '1100,1200,1300'
    union
    select '1101,1200,1301'
    union  
    select '1200,1301'
    )
    info
    go--建立存储过程
    create procedure p_test @s_sql varchar(100)
    as
    begin
    set nocount on
    declare @s_city varchar(50)
    exec('declare c_test cursor for ' + @s_sql)
    open c_test
    fetch next from c_test into @s_city
    close c_test
    deallocate c_test
    print(@s_city)
    end
    go--调用
    exec p_test 'select city from city'--删除测试数据
    drop table city
    drop procedure p_test