declare @SQLString nvarchar(500),@shopStr nvarchar(50)set @SQLString =N'declare cur_shop cursor for 
                  select shopid from shop where shopstatus=''A'' order by ' + @shopStr   
exec sp_executesql  @SQLString
open cur_shop  ......

解决方案 »

  1.   

    declare @SQLString nvarchar(500),@shopStr nvarchar(50)set @SQLString =N'declare cur_shop cursor for 
                      select shopid from shop where shopstatus=''A'' order by ' + @shopStr   
    exec sp_executesql  @SQLString
    open cur_shop  ......
      

  2.   

    declare @shopStr nvarchar(50)exec('Declare  cur_shop  Cursor 
    for select  shopid from shop where shopstatus=''A''  order by '+@shopStr   )这样看看
      

  3.   

    declare @shopStr nvarchar(50)exec('Declare  cur_shop  Cursor 
    for select  shopid from shop where shopstatus=''A''  order by '+@shopStr   )这样看看
      

  4.   


    declare @SQLString nvarchar(500),@shopStr nvarchar(50)set @SQLString =N'declare cur_shop cursor for 
                      select shopid from shop where shopstatus=''A'' order by ' + @shopStr   
    exec sp_executesql  @SQLString
    open cur_shop  ......
      

  5.   

    declare @shopStr nvarchar(50)exec('Declare  cur_shop  Cursor 
    for select  shopid from shop where shopstatus=''A''  order by '+@shopStr   )这样看看
      

  6.   


    declare @SQLString nvarchar(500),@shopStr nvarchar(50)set @SQLString =N'declare cur_shop cursor for 
                      select shopid from shop where shopstatus=''A'' order by ' + @shopStr   
    exec sp_executesql  @SQLString
    open cur_shop  ......
      

  7.   

    declare @shopStr nvarchar(50)exec('Declare  cur_shop  Cursor 
    for select  shopid from shop where shopstatus=''A''  order by '+@shopStr   )
      

  8.   

    declare @shopStr nvarchar(50)exec(' select  shopid into tmp from shop where shopstatus=''A''  order by '+ @shopStr   )
    Declare  cur_shop  Cursor 
    for select  shopid from tmp ..drop table tmp
      

  9.   


    DECLARE @id varchar(50) 
    set @id= 'id'
    exec('select top 20 * from table1 order by '+@id +' desc')
      

  10.   


    是可以的.CURSOR 的作用域是默认全局的.
    当然跟Database的选项设置有关系.
      

  11.   

    参考下BOL 中关於Cursor一节的说明.
      

  12.   

    9楼的不行,刚试过
    报错如下名为 'cur_shop' 的游标不存在。
      

  13.   


    因为你没有给@shopStr 赋值,与@SQLString 相加后@SQLString 也变成NULL了,当然就没游标啦
      

  14.   

    11楼的也不行
    exec(' select  shopid into tmp from shop where shopstatus=''A''  order by '+ @shopStr  ) 
        Declare  cur_shop  Cursor 
        for select  shopid from tmp
    open cur_shop错误如下:
       ...
    'by' 附近有语法错误。
    对象名  'tmp' 无效。
       ...
    谁有测试过的sql正确语句,快贴出来...
      

  15.   

    exec(' select  shopid into tmp from shop where shopstatus=''A''  order by '+ @shopStr  ) 
        Declare  cur_shop  Cursor 
        for select  shopid from tmp
    open cur_shop