【问题标题】:How to convert MYSQL to PL/SQL如何将 MYSQL 转换为 PL/SQL
【发布时间】:2021-12-10 01:43:00
【问题描述】:

这是我的 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?

【问题讨论】:

  • PL/SQL 中报告的语法错误(如果有)是什么?
  • 看来您需要在 Oracle DB 中使用该 SQL Select 语句。如果包含这些列的表也在 Oracle DB 中创建,它将适用于 Oracle。因为,在这个查询中没有任何与标准 SQL 语法无关的东西。另一方面,PL/SQL 代表一种在 Oracle DB 中包含一些代码块的编程语言,是一个不同的概念。

标签: mysql oracle plsql


【解决方案1】:

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;
/

正如我所说:该代码的实际外观取决于您想用它做什么。

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 2016-04-21
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2011-08-08
    • 2014-03-31
    • 2015-09-16
    相关资源
    最近更新 更多