如果在SQL查询中生成条件

时间:2014-02-27 07:58:24

标签: sql oracle

我使用oracle

我想在我的sql查询中创建条件

这是我的SQL查询:

SELECT unit.id_unit AS id, 
       unitLang.abbreviated_name_unit||  ' - ' ||unitLang.name_unit_lang
 AS name_unit_lang 
 FROM unit unit 
 LEFT OUTER JOIN employee on  employee.id_unit =  unit.id_unit
 LEFT OUTER JOIN unit_lang unitLang on  unitLang.id_unit =  unit.id_unit
 LEFT OUTER JOIN security_employee_unit secUnit ON unit.id_unit = secUnit.id_unit 
 where unitLang.lang_unit_lang  = 'ar' and 
       unit.statut = '1' and 
       employee.id_employe = 'EM-000008' or 
       secUnit.id_employee = 'EM-000008' AND 
       secUnit.type = 'S'

我想添加一个测试

这是我的测试:

如果unit.id_unit ='allorg',则查询将具有以下语法:

 SELECT unit.id_unit AS id, 
        unitLang.abbreviated_name_unit||  ' - ' ||unitLang.name_unit_lang
 AS name_unit_lang  
 FROM unit_lang unitLang, unit unit
 where unit.id_unit = unitLang.id_unit and 
       unitLang.lang_unit_lang  = 'ar' and 
       unit.statut = '1'

如何在我的查询中进行此测试

已更新:

我想开发一个函数:

在这个函数中我首先应该运行这个查询

 SELECT unit.id_unit AS id, 
           unitLang.abbreviated_name_unit||  ' - ' ||unitLang.name_unit_lang
     AS name_unit_lang 
     FROM unit unit 
     LEFT OUTER JOIN employee on  employee.id_unit =  unit.id_unit
     LEFT OUTER JOIN unit_lang unitLang on  unitLang.id_unit =  unit.id_unit
     LEFT OUTER JOIN security_employee_unit secUnit ON unit.id_unit = secUnit.id_unit 
     where unitLang.lang_unit_lang  = 'ar' and 
           unit.statut = '1' and 
           employee.id_employe = 'EM-000008' or 
           secUnit.id_employee = 'EM-000008' AND 
           secUnit.type = 'S'

然后我想进行与第一个查询结果相关的测试:

如果unit.id_unit ='allorg'

我的函数的结果应该具有此查询的结果

SELECT unit.id_unit AS id, 
            unitLang.abbreviated_name_unit||  ' - ' ||unitLang.name_unit_lang
     AS name_unit_lang  
     FROM unit_lang unitLang, unit unit
     where unit.id_unit = unitLang.id_unit and 
           unitLang.lang_unit_lang  = 'ar' and 
           unit.statut = '1'

2 个答案:

答案 0 :(得分:0)

第一个查询是否只返回一个值? 如果是,您可以将该结果分配给变量(您必须在同一函数中声明)。

declare 
var_unit_id varchar2(50);
var_other varchar2(250);
begin
SELECT unit.id_unit AS id, 
           unitLang.abbreviated_name_unit||  ' - ' ||unitLang.name_unit_lang AS name_unit_lang 
into var_unit_id, var_other

     FROM unit unit 
     LEFT OUTER JOIN employee on  employee.id_unit =  unit.id_unit
     LEFT OUTER JOIN unit_lang unitLang on  unitLang.id_unit =  unit.id_unit
     LEFT OUTER JOIN security_employee_unit secUnit ON unit.id_unit = secUnit.id_unit 
     where unitLang.lang_unit_lang  = 'ar' and 
           unit.statut = '1' and 
           employee.id_employe = 'EM-000008' or 
           secUnit.id_employee = 'EM-000008' AND 
           secUnit.type = 'S';

现在您可以查看

if var_unit_id = 'allorg' then
--put your code here
SELECT unit.id_unit AS id, 
            unitLang.abbreviated_name_unit||  ' - ' ||unitLang.name_unit_lang
     AS name_unit_lang  
     FROM unit_lang unitLang, unit unit
     where unit.id_unit = unitLang.id_unit and 
           unitLang.lang_unit_lang  = 'ar' and 
           unit.statut = '1'
end if;

但是函数只返回一个值,但结果查询返回两个值。你是如何决定获得返回值的?

答案 1 :(得分:0)

根据您的要求,只需一个查询即可

SELECT unit.id_unit AS id, 
            unitLang.abbreviated_name_unit||  ' - ' ||unitLang.name_unit_lang
     AS name_unit_lang  
     FROM unit_lang unitLang, unit unit
     where unit.id_unit = unitLang.id_unit and 
           unitLang.lang_unit_lang  = 'ar' and 
           unit.statut = '1' and
           exists (SELECT 'x' 
                         FROM unit unit 
                         LEFT OUTER JOIN employee on  employee.id_unit =  unit.id_unit
                         LEFT OUTER JOIN unit_lang unitLang on  unitLang.id_unit =  unit.id_unit
                         LEFT OUTER JOIN security_employee_unit secUnit ON unit.id_unit = secUnit.id_unit 
                         where unitLang.lang_unit_lang  = 'ar' and 
                               unit.statut = '1' and 
                               (employee.id_employe = 'EM-000008' or ecUnit.id_employee = 'EM-000008') AND 
                               secUnit.type = 'S' and 
                               unit.id_unit = 'allorg');