這是我的 MYSQL 代碼。這里我想把This轉換成PL/SQL
Select
products.productID,
products.productName,
orderDetails.quantity,
orderDetails.unitPrice,
orderDetails.unitPrice*orderDetails.quantity as sub_total,
orderDetails.discount as taxes
from products
inner join Orderdetails on products.productID=orderDetails.productID
如何將其轉換為 PL/SQL?
uj5u.com熱心網友回復:
PL/SQL 的意思是“Oracle”,因為它是 SQL 的程序擴展。換句話說,我們在 SQL 中撰寫“查詢”,在 PL/SQL 中撰寫程序、函式、包、觸發器和其他東西。
如果您只是感到困惑并且 - 實際上 - 想要在 Oracle 的 SQL 中運行該查詢,則您不必做任何事情,因為它可以正常作業(假設具有這些列的表存在于您所連接的模式中)。不過,我建議您使用表別名,因為它們使代碼更易于閱讀,例如
select
p.productid,
p.productname,
o.quantity,
o.unitprice,
o.unitprice * o.quantity as sub_total,
o.discount as taxes
from products p inner join orderdetails o on p.productid = o.productid;
如果您真的想切換到 PL/SQL,那么匿名 PL/SQL 塊就可以了(即您不需要程序或函式;您真正需要的是什么取決于您接下來要做什么)。在 PL/SQL 中,你必須選擇INTO一些東西;例如,進入本地宣告的變數。但是,由于您的查詢不包含where子句,它將回傳其productid值在兩個表中匹配的所有行,并且可以是無行、一行或多行。對于沒有行,您必須處理no_data_found例外。對于一行,它會起作用。對于許多行,您必須處理too_many_rows例外。因此,使用游標FOR回圈可能是一個好主意——這就是我將要演示的——并簡單地將找到的內容顯示在螢屏上(我將只顯示兩個值,
set serveroutput on
begin
for cur_r in (select
p.productid,
p.productname,
o.quantity,
o.unitprice,
o.unitprice * o.quantity as sub_total,
o.discount as taxes
from products p inner join orderdetails o on p.productid = o.productid
)
loop
dbms_output.put_line(cur_r.productname ||', '|| cur_r.sub_total);
end loop;
end;
/
正如我所說:該代碼的真正外觀取決于您想用它做什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338230.html
上一篇:選擇專案數量最多的每年月份
