2015-11-11 30 views
1

我想創建一個如果然後在我的PL SQL代碼,但下面的錯誤接收。我目前使用的代碼來創建一個自動序列,但當我嘗試插入下面的結果是錯誤的,因爲O_ID是Orders中的一個整數。我怎樣才能申報序列錯誤,如果然後過程

set serveroutput ON; 
DECLARE 
O_ID orders.o_id%type; 
Order_Date orders.order_date%type; 
Shipment_Date orders.shipment_date%type; 
Delivery_Date orders.delivery_date%type; 
Shipping_Method orders.Shipping_Method%type; 
Shipping_days shipping.shipping_days%type; 
Delivery_days shipping.delivery_days%type; 

Regular number := 3; 
Express_shipping number := 2; 

    Begin 
    O_ID := 'orders_seq.NEXTVAL'; 
    Order_Date := SYSDATE; 
    Shipment_Date := SYSDATE; 
Delivery_Date := SYSDATE; 
    Shipping_Method := 'Regular'; 

Shipping_days := 3; 
Delivery_days := 5; 

    IF Shipping_Method = Regular THEN 
     Shipping_Days := 3; 
     Delivery_days := 5; 

    ELSIF Shipping_Method = Express_shipping THEN 
     Shipping_Days := 1; 
     Delivery_days := 2; 
    END IF; 

    Insert into Orders(O_ID,Order_Date,Shipping_method,Shipment_date,Delivery_date,Shipping_method) 
    Values(O_ID,Order_Date,O_ID,Order_Date,Shipping_method,Shipment_date,Delivery_date,Shipping_method); 
    COMMIT; 
    dbms_output.put_line('Order ID: ' || O_ID); 
END; 

錯誤:

ERROR at line 1: 
ORA-06502: PL/SQL: numeric or value error: character to number conversion error 
ORA-06512: at line 26 
+0

似乎有點奇怪擁有的if/else反正當你賦值「正規軍」爲'Shipping_Method';但它不能與任何一個數字變量進行比較。你真的是指兩個字符串分配(即OID的序列)是字符串文字嗎? –

+0

我想能夠使用if then else語句,而不必聲明變量。如果我使用字符串,那麼希望我不需要聲明。 – LivinLife

+0

不確定你的意思是不聲明一個字符串; isn; t'O_ID'假設是序列中的數字,而不是包含序列名稱的字符串文字?你正在比較的值從哪裏來 - 這將成爲一個存儲過程,他們將被傳入?添加訂單表的定義可能也會有所幫助,以澄清您所期望的數據類型。 –

回答

1

這個問題在SHIPPING_METHOD的變量。如果他們是一個字符串,那麼你不能做到這一點比較

IF Shipping_Method = Regular THEN 

this should be 

IF Shipping_Method = 'Regular' THEN 

你可以聲明常數VARCHAR2(20)代表的運輸方式,這將減少錯誤的機會。

DECLARE 
c_shipping_regular CONSTANT VARCHAR2(20) := 'Regular'; 

BEGIN 
IF Shipping_Method = c_shipping_regular THEN 
.... 
+0

似乎我聲明序列錯誤的O_ID。 聲明 O_ID int:='orders_seq.NEXTVAL';開始 O_ID:='orders_seq.NEXTVAL'; 或 \t DECLARE \t O_ID orders.o_id%type;開始 O_ID:='orders_seq.NEXTVAL'; – LivinLife

+0

代碼中有一個名爲'regular'的變量,所以我認爲該行確實沒問題。 –

+0

@WilliamRobertson同意,令人困惑的是有一個名爲regular的變量和一個固定的字符串'Regular',所以我仍然會重構使用常量和命名約定。 – kevinsky

0

錯誤消息表明您正試圖爲數字字段指定一個非數字值。看看你的插入語句看起來不對。您將o_id,order_date兩次放入values()中。

insert into orders 
    (o_id 
    ,order_date 
    ,shipping_method 
    ,shipment_date 
    ,delivery_date 
    ,shipping_method) 
values 
    (o_id 
    ,order_date 
    ,o_id 
    ,order_date 
    ,shipping_method 
    ,shipment_date 
    ,delivery_date 
    ,shipping_method); 
+0

我試圖輸入我聲明的O_ID字段:='orders_seq.NEXTVAL';我的目標是在我爲新訂單執行程序時分配唯一的ID。 – LivinLife

1

您在代碼中有幾個錯誤:

DECLARE 
O_ID orders.o_id%type; 
Order_Date orders.order_date%type; 
Shipment_Date orders.shipment_date%type; 
Delivery_Date orders.delivery_date%type; 
Shipping_Method orders.Shipping_Method%type; 
Shipping_days shipping.shipping_days%type; 
Delivery_days shipping.delivery_days%type; 

Regular number := 3; 
Express_shipping number := 2; 

Begin 
    --O_ID := 'orders_seq.NEXTVAL'; 
    -- Must be 
    O_ID := orders_seq.NEXTVAL; 
    Order_Date := SYSDATE; 
    Shipment_Date := SYSDATE; 
    Delivery_Date := SYSDATE; 
    --Shipping_Method := 'Regular'; 
    -- Either use the number or define constants and use them 
    Shipping_Method := 3; 

    Shipping_days := 3; 
    Delivery_days := 5; 

    IF Shipping_Method = Regular THEN 
     Shipping_Days := 3; 
     Delivery_days := 5; 

    ELSIF Shipping_Method = Express_shipping THEN 
     Shipping_Days := 1; 
     Delivery_days := 2; 
    END IF; 

    Insert into Orders(
     O_ID, 
     Order_Date, 
     Shipping_method, 
     Shipment_date, 
     Delivery_date, 
     Shipping_method) 
    Values(
     O_ID, 
     Order_Date, 
     --O_ID, 
     --Order_Date, 
     Shipping_method, 
     Shipment_date, 
     Delivery_date, 
     Shipping_method); 
    COMMIT; 
    dbms_output.put_line('Order ID: ' || O_ID); 
END; 
0

你試圖設置變量,應該是使用字符串文字數字。在您的實驗中,您嘗試通過regular變量將您的shipping_method變量的值設置爲3;但因爲你已經把它放在單引號中,所以你要分配一個字符串文字('Regular')而不是一個數字(Regular沒有引號,或者只是3)。你正在用O_ID做同樣的事情,分配字符串文字而不是調用序列nextval的結果。我想你的意思做:

O_ID := orders_seq.NEXTVAL; 
Shipping_Method := Regular; 

但你的評論說,shipping_method列實際上是一個字符串,所以宣佈regularexpress_shipping爲數字,並將其設置爲2和3,沒有按」真的有道理。如果他們應該是字符串值,然後聲明並適當地配置它們,使用任何值,您實際上在該列中:

l_Regular orders.Shipping_Method%type := 'REGULAR'; 
l_Express_shipping orders.Shipping_Method%type := 'EXPRESS'; 

分配,然後比較會更有意義,但正如你使用固定的值,在這似乎是多餘的 - 大概shipping_method將最終從別的地方來。

DECLARE 
    l_O_ID orders.o_id%type; 
    l_Order_Date orders.order_date%type; 
    l_Shipment_Date orders.shipment_date%type; 
    l_Delivery_Date orders.delivery_date%type; 
    l_Shipping_Method orders.Shipping_Method%type; 
    l_Shipping_days shipping.shipping_days%type; 
    l_Delivery_days shipping.delivery_days%type; 

    l_Regular orders.Shipping_Method%type := 'REGULAR'; 
    l_Express_shipping orders.Shipping_Method%type := 'EXPRESS'; 

Begin 
    l_O_ID := orders_seq.NEXTVAL; 
    l_Order_Date := SYSDATE; 
    l_Shipment_Date := SYSDATE; 
    l_Delivery_Date := SYSDATE; 
    l_Shipping_Method := l_Regular; 

    l_Shipping_days := 3; 
    l_Delivery_days := 5; 

    IF l_Shipping_Method = l_Regular THEN 
    l_Shipping_Days := 3; 
    l_Delivery_days := 5; 
    ELSIF l_Shipping_Method = l_Express_shipping THEN 
    l_Shipping_Days := 1; 
    l_Delivery_days := 2; 
    END IF; 

    Insert into Orders(O_ID,Order_Date,Shipping_method,Shipment_date, 
    Delivery_date) 
    Values(l_O_ID,l_Order_Date,l_Shipping_method,l_Shipment_date, 
    l_Delivery_date); 

    dbms_output.put_line('Order ID: ' || l_O_ID); 
END; 

你也想太多值插入到表你會反覆O_IDOrder_DateShipping_method;所以我刪除了額外的參考。

使用列名作爲變量名可能會導致混淆和錯誤(尤其是更新),所以我將它們全部用l_作爲前綴 - 任何約定都會執行。你並不是真的需要它們;你可以有直接的順序和SYSDATE引用在插入代替:

Insert into Orders(O_ID,Order_Date,Shipping_method,Shipment_date, 
    Delivery_date) 
    Values(orders_seq.NEXTVAL,SYSDATE,l_Shipping_method,SYSDATE, 
    SYSDATE); 

    dbms_output.put_line('Order ID: ' || orders_seq.CURRVAL); 
+0

shipping_method列是varchar,但我在該表中有2個其他列,用於聲明每個裝運方法的裝運和交貨日期。發貨日期和送貨日期列是編號。 – LivinLife

+0

@JRock - 你將在shipping_method中擁有什麼樣的價值?爲什麼你要與數字(2和3)進行比較? –

+0

亞歷克斯 - 我重新做了我的表,以便每種運輸方式現在都有一個ID。 IF S_ID = 1 THEN Shipping_Days:= 3; Delivery_days:= 5; Shipping_cost:= 5; ELSIF S_ID = 2 THEN Shipping_Days:= 1; Delivery_days:= 2; Shipping_Cost:= 20; END IF; – LivinLife