这是我实际代码的一个简单的条带,但我想不知道如何做到这一点,所以我可以应用它。我有这样的代码:
declare
use_t3 varchar2(1): = 'N'; //Y or N
use_t4 varchar2(1): = 'N'; //Y or N
begin
INSERT INTO MY_BIG_TABLE
SELECT
t3.qid AS "QID",
(SELECT t5.flag FROM t5 WHERE(t5.qid = t4.qid)) AS "FLAG"
FROM
t1 p,
t2 pm,
t3 pms,
t4 pmo
WHERE
t1.id = t2.qid
AND t3.day = 'Monday'
AND t2.id = t4.pmd_id(+)
AND t4.date IS NULL
end;
现在我有两个变量:
use_t3
use_t4
如果use_t3和use_t4都等于'Y'(这意味着是,请使用这些表),那么查询应该按照两个表应该使用的完全相同的方式运行(只有use_t1或use_t2可以等于' N',这意味着总会有一个属于'Y'的人。
如果use_t3 ='N'且use_t4 ='Y',那么我希望t3.qid列仍然显示但返回null,并且条件为:
t3.day = 'Monday'
不使用。
如果use_t3 ='Y'且use_t4 ='N',那么我希望“FLAG”列返回空白值和条件:
t2.id = t4.pmd_id(+)
AND t4.date IS NULL
不使用。
我希望这是有道理的。
干杯
答案 0 :(得分:0)
要有条件地连接到表,ANSI LEFT JOIN中的case语句应该可以正常工作,只要导出就可以在两个表上使用Left Join。
declare
use_t3 varchar2(1): = 'N'; //Y or N
use_t4 varchar2(1): = 'N'; //Y or N
begin
INSERT INTO MY_BIG_TABLE
SELECT
t3.qid AS "QID",
(SELECT T5.FLAG FROM T5 WHERE(T5.QID = T4.QID)) AS "FLAG"
FROM T1 /*P*/
JOIN T2 /*PM*/ ON T1.ID = T2.QID
LEFT JOIN T3 /*pms*/ ON CASE WHEN USE_T3 = 'Y' AND T3.DAY = 'Monday' THEN 1 ELSE 0 END = 1
LEFT JOIN t4 /*pmo*/ ON CASE WHEN USE_T4 = 'Y' AND T2.ID = T4.PMD_ID AND t4.date IS NULL THEN 1 ELSE 0 END = 1
end;
否则,您可能不得不做多个IF THEN语句来涵盖每个选项。
declare
USE_T3 VARCHAR2(1): = 'N'; --//Y or N
use_t4 varchar2(1): = 'N'; --//Y or N
BEGIN
--If both are 'Y' ---------------------
IF USE_T3 = 'Y' AND USE_T4 = 'Y' then
INSERT INTO MY_BIG_TABLE
SELECT
t3.qid AS "QID",
(SELECT t5.flag FROM t5 WHERE(t5.qid = t4.qid)) AS "FLAG"
FROM
t1 p,
t2 pm,
t3 pms,
t4 pmo
WHERE
t1.id = t2.qid
AND t3.day = 'Monday'
AND t2.id = t4.pmd_id(+)
AND T4.DATE IS NULL
END IF;
--If USE_T3 is 'Y' And USE_T4 is 'N' ---------------------
if IF USE_T3 = 'Y' AND USE_T4 = 'N' then
INSERT INTO MY_BIG_TABLE
SELECT
t3.qid AS "QID",
NULL AS "FLAG"
FROM
t1 p,
t2 pm,
t3 pms
WHERE
t1.id = t2.qid
AND t3.day = 'Monday'
END IF;
--If USE_T3 is 'N' And USE_T4 is 'Y' ---------------------
IF USE_T3 = 'N' AND USE_T4 = 'Y' then
INSERT INTO MY_BIG_TABLE
SELECT
NULL AS "QID",
(SELECT t5.flag FROM t5 WHERE(t5.qid = t4.qid)) AS "FLAG"
FROM
t1 p,
t2 pm,
t4 pmo
WHERE
t1.id = t2.qid
AND t2.id = t4.pmd_id(+)
AND T4.DATE IS NULL
end if;
end;
答案 1 :(得分:0)
你可以使用字符串连接..
DECLARE
use_t3 varchar2(1): = 'N'; //Y or N
use_t4 varchar2(1): = 'N'; //Y or N
plsql_block VARCHAR2(500);
BEGIN
-- Dynamic PL/SQL block invokes insert statement:
plsql_block := 'INSERT INTO MY_BIG_TABLE '
||' SELECT ';
If use_t3 = 'Y' then
plsql_block :=
plsql_block || ' t3.qid AS "QID", ';
Else
plsql_block :=
plsql_block || ' null AS "QID", ';
End if;
If use_t4 = 'Y'
plsql_block :=
plsql_block || ' (SELECT t5.flag FROM t5 WHERE(t5.qid = t4.qid)) AS "FLAG"';
Else
plsql_block :=
plsql_block || ' '''' AS "FLAG", ';
End if;
plsql_block := plsql_block || ' FROM t1 p, t2 pm ';
If use_t3 = 'Y' then
plsql_block :=
plsql_block || ' ,t3 pms ';
End if
If use_t4 = 'Y' then
plsql_block :=
plsql_block || ' ,t4 pmo ';
End if
plsql_block := plsql_block || ' WHERE t1.id = t2.qid ';
If use_t3 = 'Y' then
plsql_block :=
plsql_block || ' AND t3.day = ''Monday'' ';
End if
If use_t4 = 'Y' then
plsql_block :=
plsql_block || ' AND t2.id = t4.pmd_id(+) AND t4.date IS NULL ' ;
End if
EXECUTE IMMEDIATE plsql_block;
END;