2014-07-02 56 views
0

在Sql開發者中,我需要存儲變量並在以後使用它如何做到這一點?oracle中的全局變量

VARIABLE liab NUMBER; 
    exec select nvl(LIAB_ID,-1)into liab FROM GETM_LIAB_CUST WHERE CUSTOMER_NO = (select customer_id from apps_master where ACCOUNT_NUMBER='&C' AND BRANCH_CODE = '&B') ; 

SPOOL c:\query.SQL 
select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID =liab ;' FROM DUAL; 
spool off 

試圖

column liabid new_value liab noprint 
select nvl(LIAB_ID,-1) liabid FROM GETM_LIAB_CUST WHERE CUSTOMER_NO = (select customer_id from apps_master where ACCOUNT_NUMBER='&C' AND BRANCH_CODE = '&B') ; 

它不工作。請幫我

+1

究竟「它不起作用」是什麼意思?錯誤消息,缺少/錯誤輸出,爆炸煙花......? –

+0

Hi Frank - ORA-06550:第2行,第34列: PLS-00201:標識符'LIAB'必須聲明 ORA-06550:第2行第39列: PL/SQL:ORA-00904::無效標識符 ORA-06550:第2行,第7列: PL/SQL:SQL語句被忽略 – user3737084

+0

請不要在註釋中加入其他信息。改爲編輯你的問題(使用「編輯」按鈕)。 –

回答

0

VARIABLE declares a bind variable,所以你需要使用一個冒號前綴它時,你參考一下吧:

VARIABLE liab NUMBER; 
exec select nvl(LIAB_ID,-1) into :liab FROM ... 

SPOOL c:\query.SQL 
select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' FROM DUAL; 
spool off 

您也可以使用替代變量而不是綁定變量來執行此操作:

column liab new_value g_liab 
select nvl(LIAB_ID,-1) as liab FROM ... 

SPOOL c:\query.SQL 
select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = &g_liab;' FROM DUAL; 
spool off 

column x new_value y syntax該定義一個替換變量&y,其值是從列設置在後續查詢稱爲x

在這兩種情況下,SQL Developer將放在兩個頭,你跑進後臺打印文件的命令,所以你會看到這一點:

> select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' from dual 
'SELECT*FROMGETB_UTILSWHERELIAB_ID='||:LIAB||';'         
---------------------------------------------------------------------------------- 
SELECT * FROM GETB_UTILS WHERE LIAB_ID = 1;           

> spool off 

您可以添加set heading offset echo off,但如果你運行它直接從SQL工作表,你還是會結束:

> select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' from dual 
SELECT * FROM GETB_UTILS WHERE LIAB_ID = 1;           

爲了擺脫你需要它保存爲一個腳本,然後運行從另一個工作表中回顯命令,因爲是與您以前的問題。所以,如果你有一個名爲liab.sql腳本包含:

VARIABLE liab NUMBER; 
set feedback off 
exec select nvl(LIAB_ID,-1) into :liab FROM ... 

set echo off 
set heading off 
SPOOL c:\query.SQL 
select 'SELECT * FROM GETB_UTILS WHERE LIAB_ID = ' || :liab || ';' FROM DUAL; 
spool off 

...然後跑去與

@liab.sql 

...的query.sql文件將包含剛:

SELECT * FROM GETB_UTILS WHERE LIAB_ID = 1;          
+0

Working.Thank you Alex :) – user3737084

+0

@ user3737084 - 正如您在上一個問題中所述,您需要將其作爲腳本運行,以允許'set echo off'生效,因爲它與'set termout'的行爲相同。我已經更新了答案以顯示差異。 –