oracle:带案例的程序

时间:2014-07-23 11:31:09

标签: oracle sql-order-by case procedure

为什么这个程序不起作用? 我试图通过游标获取表格的行:AF_EMPLOYEE,这将使用ASC / DESC(根据情况)订购。

Create or Replace Procedure return_rows_EMPLOYEE (in_order in number, 
in_sorting in VARCHAR2, in_job in VARCHAR2, out_cursor OUT SYS_REFCURSOR) As
Begin
  OPEN out_cursor for
  select * from AF_EMPLOYEE
  WHERE EMPL_JOB_TITLE = in_job
  ORDER BY in_sorting
  CASE in_order
  WHEN 1 THEN   ASC
  ELSE  DESC
  END
End; 

2 个答案:

答案 0 :(得分:0)

如果排序列是数字,您可以使用case来否定它,但这不是好代码!

CREATE OR REPLACE PROCEDURE return_rows_EMPLOYEE (in_order in number, 
in_sorting in VARCHAR2, in_job in VARCHAR2, out_cursor OUT SYS_REFCURSOR) As
Begin
  OPEN out_cursor for
  select * from AF_EMPLOYEE
  WHERE EMPL_JOB_TITLE = in_job
  ORDER BY 
    CASE in_sorting WHEN 'ASC'  THEN  sort_column 
                    WHEN 'DESC' THEN -sort_column 
     END;
END; 
/

答案 1 :(得分:-1)

Create or Replace Procedure return_rows_EMPLOYEE (in_order in number, 
in_sorting in VARCHAR2, in_job in VARCHAR2, out_cursor OUT SYS_REFCURSOR) As
Begin
  if in_order = 1 THEN
     OPEN out_cursor for
     select * from AF_EMPLOYEE
     WHERE EMPL_JOB_TITLE = in_job
     ORDER BY in_sorting ASC
  else
     OPEN out_cursor for
     select * from AF_EMPLOYEE
     WHERE EMPL_JOB_TITLE = in_job
     ORDER BY in_sorting DESC
  end if;
End; 

<强>更新 试试这个:

Create or Replace Procedure return_rows_EMPLOYEE (in_order in number, 
in_sorting in VARCHAR2, in_job in VARCHAR2, out_cursor OUT SYS_REFCURSOR) As
Begin
  OPEN out_cursor for
  select * from AF_EMPLOYEE
  WHERE EMPL_JOB_TITLE = in_job
  ORDER BY 
  case when in_order = 1 then
      in_sorting 
  end,
  case when in_order <> 1 then
      in_sorting
  end desc;
End;