http://topic.csdn.net/t/20040629/23/3132813.htmlDECLARE   @cursor   INT   
  DECLARE   @rowcount   INT   
    
  EXEC   sp_cursoropen   @cursor   OUTPUT,   N'SELECT   *   FROM   customers   where   cust_id=@id',   2,   8193,   @rowcount   output,   N'@id',   @id=10   
    
  EXEC   sp_cursorclose   @cursor这个@id如何传入?

解决方案 »

  1.   

    服务器: 消息 137,级别 15,状态 2,行 1
    必须声明变量 '@id'。
    服务器: 消息 16945,级别 16,状态 1,过程 sp_cursoropen,行 5
    游标未声明。
    服务器: 消息 16909,级别 16,状态 1,过程 sp_cursorclose,行 7
    sp_cursorclose: 所提供的游标标识符值(0)无效。
      

  2.   

    http://jtds.sourceforge.net/apiCursors.html#_sp_cursoropen上面的连接里的例子搞不通。
    就是这种语句:USE pubs-- Create a dynamc read-only cursor
    DECLARE @cursor INTEXEC sp_cursoropen @cursor OUTPUT, N'SELECT * FROM myTable WHERE col1=@P1 AND col2 LIKE @P2', 2, 8193, N'@P1 INT, @P2 VARCHAR(255)', 10, '%x%' -- Close the cursor
    EXEC sp_cursorclose @cursor
      

  3.   

    显式指定参数,并将@id的定义提前放:
    DECLARE   @cursor   INT   
    DECLARE   @rowcount   INT   EXEC   sp_cursoropen  @cursor=@cursor   OUTPUT
    ,@paramdef=N'@id int'
    ,@id=10
    ,@stmt=N'SELECT   *   FROM   ta   where   id=@id'
    ,@scrollopt=2
    ,@ccopt=8193
    ,@rowcount=@rowcount  output
    EXEC   sp_cursorclose   @cursor
    不过又报这个错,不知道怎么解决:/*
    消息 16902,级别 16,状态 24,过程 sp_cursoropen,第 1 行
    sp_cursoropen: 参数 'scrollopt' 的值无效。
    */
      

  4.   

    DECLARE   @cursor   INT   
    DECLARE   @rowcount   INT   EXEC   sp_cursoropen      @cursor   OUTPUT                        
                            , N'SELECT   *   FROM   t2   where   a=@id'
                            , 135170
                            , 8193
                            , @rowcount  output
    ,N'@id int'
                            ,@id=8
    EXEC   sp_cursorclose   @cursor
      

  5.   

    DECLARE   @cursor   INT   
    DECLARE   @rowcount   INT   EXEC   sp_cursoropen      @cursor   OUTPUT                        
                            , N'SELECT   *   FROM   t2   where   a=@id'
                            , 4098
                            , 8193
                            , @rowcount  output
    ,N'@id int'
                            ,@id=2
    EXEC sp_cursorfetch @cursor, 2, 0, 3
    EXEC   sp_cursorclose   @cursor/*
    a           b
    ----------- -----------a           b
    ----------- -----------
    2           6
    */
      

  6.   

    这样也行DECLARE   @cursor   INT   
    DECLARE   @rowcount   INT   EXEC   sp_cursoropen      @cursor   OUTPUT                        
                            , N'SELECT   *   FROM   t2   where   a=@id'
                            , 135170--4098
                            , 8193
                            , @rowcount  output
    ,N'@id int'
                            ,@id=2
    EXEC sp_cursorfetch @cursor, 2, 0, 3
    EXEC   sp_cursorclose   @cursor/*
    a           b
    ----------- -----------a           b
    ----------- -----------
    2           6
    */
      

  7.   

     N'@id'@id 没有类型么?加个类型试试
      

  8.   

    前段时间做了写了几个类似的存储过程
    @id可以做为传入值,通过定义一个字符传保存起来
    然后把select语句作为字符传保存起来
    然后把两个字符传连接起来,就ok了。详细交流可以msg。
      

  9.   

    [@scrollopt =] scroll_options OUTPUT 
    Is the cursor scroll type. scroll_options is int with a default of 1 (keyset-driven), and can be a combination of these values (exactly one of the first 5 must be specified).Value Description 
    0x0001 Keyset-driven cursor. 
    0x0002 Dynamic cursor. 
    0x0004 Forward-only cursor. 
    0x0008 Static cursor. 
    0x0010 Fast forward-only cursor. 
    0x1000 Parameterized query. ! 0x1000 Parameterized Query !
    居然没看到。