2016-09-23 29 views
1

我需要在需要使用常量的位置運行查詢(以便我們可以更改其值並直接影響查詢中使用的值)。聲明要在查詢中使用的常量

declare 
QTY_TRESHOLD CONSTANT NUMBER(10,0) := 1; 
begin 
    select * from FD111200_DBF where M_NB = QTY_TRESHOLD 
end; 

如果我嘗試運行上面,我得到的第一個語句以下錯誤:

An error occurred when executing the SQL command: 
declare 
QTY_TRESHOLD CONSTANT NUMBER(10,0) := 1 

ORA-06550: line 2, column 39: 
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: 

    * & = - + ; </> at in is mod remainder not rem 
    <an exponent (**)> <> or != or ~= >= <= <> and or like like2 
    like4 likec between || multiset member submultiset [SQL State=65000, DB Errorcode=6550] 

Execution time: 0.01s 

1 statement(s) failed. 

我在做什麼錯?看看Oracle的the sample,看起來這應該是好的語法。 任何人都可以幫助我理解?

+1

a選擇塊中需要一個INTO。你錯過了一個;在選擇聲明 – Aleksej

+0

@Aleksej嘗試過,但它沒有解決問題。該請求在第一條語句中仍然失敗。 –

+2

你從哪裏運行?它看起來像是第一個分號被看作是語句分隔符。這看起來像一個客戶問題。 [[如此處所示](http://stackoverflow.com/q/33617220/266304),但其他客戶端不處理PL/SQL或需要配置來處理它)。 –

回答

1

更多評論而非答案,但對評論太長; 我只是想你的代碼添加INTO;

SQL> CREATE TABLE FD111200_DBF(M_NB NUMBER); 

Table created. 

SQL> insert into FD111200_DBF values (1); 

1 row created. 

SQL> declare 
    2  QTY_TRESHOLD CONSTANT NUMBER(10,0) := 1; 
    3  var number; 
    4 begin 
    5  select M_NB 
    6  into var 
    7  from FD111200_DBF where M_NB = QTY_TRESHOLD; 
    8  -- 
    9  dbms_output.put_line(var); 
10 end; 
11/
1 

PL/SQL procedure successfully completed.