使用ACCEPT,CASE在pl / sql中创建CURSOR

时间:2015-01-23 19:09:24

标签: sql oracle plsql

我正在尝试创建一个脚本,允许用户在收集学生联系信息时从ACCEPT中选择要使用的CASE人口。

PROMPT 'Select a popluation for emails'
PROMPT '1. Currently registered'
PROMPT '2. New Applicants'
PROMPT
ACCEPT cnt number PROMPT 'Selection: ';
...
CURSOR stu_lst IS  
CASE &cnt
    WHEN 1 THEN  -- Current registered students.
        select distinct SFRSTCA_PIDM pidm
        from   SFRSTCA
        where  SFRSTCA_TERM_CODE = '201403' and
               SFRSTCA_LEVL_CODE = '01' and
               SFRSTCA_RSTS_CODE = 'RE';
    WHEN 2 THEN  -- New applicants
        select app_pidm pidm
        from   app
        where  app_term = 'Fall 2014';
    ELSE
        -- Incorrect selection.
        DBMS_OUTPUT.PUT_LINE('Incorrect selection made.');
        exit;
    END;
END;

1 个答案:

答案 0 :(得分:0)

假设两个查询返回相同的数据类型,您可以使用带有过滤器的联合来检查每个部分中的变量;类似的东西:

DECLARE
  CURSOR stu_lst IS
    -- Current registered students.
    select distinct SFRSTCA_PIDM pidm
    from   SFRSTCA
    where  &cnt = 1 and
           SFRSTCA_TERM_CODE = '201403' and
           SFRSTCA_LEVL_CODE = '01' and
           SFRSTCA_RSTS_CODE = 'RE';
    UNION ALL
    -- New applicants
    select app_pidm
    from   app
    where  &cnt = 2 and
           app_term = 'Fall 2014';

  invalid_argument EXCEPTION;
  ...
BEGIN
  IF &cnt NOT IN (1, 2) THEN
    RAISE invalid_argument;
  END IF

  FOR rec IN stu_lst LOOP
    h_pidm := rec.pidm;
    ...
  END LOOP;

EXCEPTION
  WHEN invalid_argument THEN
    dbms_output.put_line('Incorrect selection made.');
END;
/

您还可以声明一个游标变量,并使用相应的查询在块的主体内的case语句中打开它。这虽然坚持你的显式游标语法。