如何在联盟中动态执行其中一个查询?

时间:2011-10-24 09:33:07

标签: sql oracle oracle10g

我想根据参数执行这两个查询中的一个。 参数可以是是/否标志。根据“是”值,第一个查询应该执行,第二个查询应该执行“否”。

SELECT per.PERSONAL_NUM as EMP_NUM,per.FIRST_NAME||' '||MIDDLE_NAME||' '||LAST_NAME AS NAME,per.EMPLOYEE_TYPE
  FROM PER_EMPLOYEE_MST per
  WHERE FIRST_NAME LIKE (:pname)||'%'

  UNION ALL 

  SELECT dep.DEPUT_PERS_NUM as EMP_NUM,dep.FIRST_NAME||' '||MIDDLE_NAME||' '||LAST_NAME AS NAME,dep.EMPLOYEE_TYPE
  FROM PER_DEPUTATION_MST dep
  WHERE FIRST_NAME LIKE (:pname)||'%'

  ORDER BY NAME

1 个答案:

答案 0 :(得分:2)

检查每个部分的WHERE子句的标志部分,因此如果它们不合适,它们将不返回任何行:

SELECT per.PERSONAL_NUM as EMP_NUM,per.FIRST_NAME||' '||MIDDLE_NAME||' '||LAST_NAME AS NAME,per.EMPLOYEE_TYPE FROM PER_EMPLOYEE_MST per WHERE FIRST_NAME LIKE (:pname)||'%' AND :flag = 1

UNION ALL

SELECT dep.DEPUT_PERS_NUM as EMP_NUM,dep.FIRST_NAME||' '||MIDDLE_NAME||' '||LAST_NAME AS NAME,dep.EMPLOYEE_TYPE FROM PER_DEPUTATION_MST dep WHERE FIRST_NAME LIKE (:pname)||'%' AND :flag = 0

ORDER BY NAME