CURSOR_STATUS
这是一个标量函数,该函数允许存储过程的调用方确定针对一个给定参数,该过程是否返回游标和结果集。语法
CURSOR_STATUS
    ( 
        { 'local' , 'cursor_name' }
        | { 'global' , 'cursor_name' }
        | { 'variable' , 'cursor_variable' } 
    ) 

解决方案 »

  1.   

    示例
    下面的示例创建一个名为 lake_list 的过程,并将执行 lake_list 的输出结果用作 CURSOR_STATUS 的检验。说明  本示例依赖于一个名为 check_authority 的过程,该过程尚未创建。
    USE pubs
    IF EXISTS (SELECT name FROM sysobjects
          WHERE name = 'lake_list' AND type = 'P')
       DROP PROCEDURE lake_list
    GO
    CREATE PROCEDURE lake_list
       ( @region varchar(30),
         @size integer,
         @lake_list_cursor CURSOR VARYING OUTPUT )
    AS 
    BEGIN
       DECLARE @ok SMALLINT
       EXECUTE check_authority @region, username, @ok OUTPUT
       IF @ok = 1
          BEGIN
          SET @lake_list_cursor =CURSOR LOCAL SCROLL FOR
             SELECT name, lat, long, size, boat_launch, cost
             FROM lake_inventory
             WHERE locale = @region AND area >= @size
             ORDER BY name
          OPEN @lake_list_cursor
          END
    END
    DECLARE @my_lakes_cursor CURSOR
    DECLARE @my_region char(30)
    SET @my_region = 'Northern Ontario'
    EXECUTE lake_list @my_region, 500, @my_lakes_cursor OUTPUT
    IF Cursor_Status('variable', '@my_lakes_cursor') <= 0
       BEGIN
       /* Some code to tell the user that there is no list of
       lakes for him/her */
       END
    ELSE
       BEGIN
          FETCH @my_lakes_cursor INTO -- Destination here
          -- Continue with other code here.
    END