2016-01-17 58 views
0

我下面的查詢:在Oracle中創建一個查詢,以取代變量

SELECT c.accountid, c.accountnum 
FROM account o, [email protected] c 
WHERE o.accountid = c.accountid and c.date > sysdate; 

我登錄鄰數據中心」數據庫,以及從C數據中心的數據庫中提取信息,我需要一個簡單的PL/SQL查詢如下:

declare 
variable 

begin 
statements... 
.... 

end 

我不希望任何程序或功能。

我想要做的就是用[email protected] csysdate作爲變量。

select c.accountid, c.accountnum 
from account o, (variable) 
where o.accountid = c.accountid and c.date > (variable); 

我該怎麼做?

回答

0

甲骨文設置

CREATE TABLE accounts (accountid, accountnum) AS 
    SELECT LEVEL, LEVEL FROM DUAL CONNECT BY LEVEL < 11; 

CREATE TABLE accounts2 (accountid, accountnum, "date") AS 
    SELECT 2*LEVEL, 2*LEVEL, SYSDATE + 1 FROM DUAL CONNECT BY LEVEL < 6; 

PL/SQL匿名塊

SET SERVEROUTPUT ON; 
/
DECLARE 
    TYPE acc_cursor IS REF CURSOR; 
    table_name VARCHAR2(100) := 'accounts2'; 
    datetime DATE   := SYSDATE; 
    v_cursor acc_cursor; 
    v_accid  NUMBER; 
    v_accnum NUMBER; 
BEGIN 
    OPEN v_cursor FOR 
    'SELECT c.accountid, c.accountnum ' 
    || 'FROM accounts o ' 
    || 'INNER JOIN ' || table_name || ' c ' 
    || 'ON (o.accountid = c.accountid) ' 
    || 'WHERE c."date" > :dt' 
    USING datetime; 

    -- Fetch rows from result set one at a time: 
    LOOP 
    FETCH v_cursor INTO v_accid, v_accnum; 
    EXIT WHEN v_cursor%NOTFOUND; 

    DBMS_OUTPUT.PUT_LINE(v_accid || ' - ' || v_accnum); 
    END LOOP; 

    CLOSE v_cursor; 
END; 
/