Oracle存储过程(如果不存在)返回

Oracle存储过程(如果不存在)返回,oracle,if-statement,procedure,not-exists,Oracle,If Statement,Procedure,Not Exists,我是一个严格意义上的MS SQL使用者,但我的任务是编写一个Oracle存储过程,比较两个表中缺少的数据。我需要写一些东西,如果订单丢失,将返回1。到目前为止,我有以下几点,但它甚至还没有接近工作。我在网上搜遍了,结果很小。任何帮助都将不胜感激 CREATE OR REPLACE PROCEDURE SP_PO_CHECK AS BEGIN IF NOT EXISTS ( SELECT PO FROM ORDERS, PO_LIST, WHERE Order_PO = PO; ) THE

我是一个严格意义上的MS SQL使用者,但我的任务是编写一个Oracle存储过程,比较两个表中缺少的数据。我需要写一些东西,如果订单丢失,将返回1。到目前为止,我有以下几点,但它甚至还没有接近工作。我在网上搜遍了,结果很小。任何帮助都将不胜感激

CREATE OR REPLACE PROCEDURE SP_PO_CHECK


AS

BEGIN

IF NOT EXISTS ( SELECT PO FROM ORDERS, PO_LIST, WHERE Order_PO = PO; ) THEN RETURN 0; ELSE RETURN 1

END;

试着这样做:

RETURN CASE WHEN (SELECT COUNT(*) FROM ORDERS, PO_LIST, WHERE Order_PO = PO; ) <> (SELECT COUNT(*) FROM ORDERS) THEN 0 ELSE 1 END

试着这样做:

RETURN CASE WHEN (SELECT COUNT(*) FROM ORDERS, PO_LIST, WHERE Order_PO = PO; ) <> (SELECT COUNT(*) FROM ORDERS) THEN 0 ELSE 1 END

首先,它需要的是功能而不是程序

然后需要定义如何在两个表中检查存在性。您可以使用master table left连接到detail table,并检查它对于我使用rowid的某些强制列是否返回NOTNULL,因为它始终不为NULL。 或者可以使用notexists子句

然后需要使用into子句将结果返回到PL_SQL变量中 最后返回变量作为函数的结果

CREATE OR REPLACE FUNCTION SP_PO_CHECK
RETURN NUMBER
AS
 l_total_cnt number; 
 l_match_cnt number;
BEGIN
  select count(*), -- total count of record only in po_list. I assume that this is your drive table. If not you can switch tables or use full outer join
         count(orders.rowid) -- count of records exist in both tables
    into l_total_cnt, l_match_cnt
    from po_list
    left join orders
      on Order_PO = PO;

  if l_total_cnt =  l_match_cnt then 
    return 1;
  else 
    return 0; 
  end if;

END SP_PO_CHECK;

首先,它需要的是功能而不是程序

然后需要定义如何在两个表中检查存在性。您可以使用master table left连接到detail table,并检查它对于我使用rowid的某些强制列是否返回NOTNULL,因为它始终不为NULL。 或者可以使用notexists子句

然后需要使用into子句将结果返回到PL_SQL变量中 最后返回变量作为函数的结果

CREATE OR REPLACE FUNCTION SP_PO_CHECK
RETURN NUMBER
AS
 l_total_cnt number; 
 l_match_cnt number;
BEGIN
  select count(*), -- total count of record only in po_list. I assume that this is your drive table. If not you can switch tables or use full outer join
         count(orders.rowid) -- count of records exist in both tables
    into l_total_cnt, l_match_cnt
    from po_list
    left join orders
      on Order_PO = PO;

  if l_total_cnt =  l_match_cnt then 
    return 1;
  else 
    return 0; 
  end if;

END SP_PO_CHECK;

识别这种情况的正确查询是:

select count(*)
from   po_list p
where  not exists (
         select 1
         from   orders o
         where  o.order_po = p.po)
  and  rownum = 1;
这将在发现问题po后立即停止查询,并返回0或1

因此,将其选择为变量并返回1-其值:

create or replace function missing_order
as
  missing_po_found integer;
begin
  select count(*)
  into   missing_po_found
  from   po_list p
  where  not exists (
           select 1
           from   orders o
           where  o.order_po = p.po)
  and    round = 1;

  return 1 - missing_o_found;
end;

未运行,因此不能100%确定没有输入错误。

识别这种情况的正确查询是:

select count(*)
from   po_list p
where  not exists (
         select 1
         from   orders o
         where  o.order_po = p.po)
  and  rownum = 1;
这将在发现问题po后立即停止查询,并返回0或1

因此,将其选择为变量并返回1-其值:

create or replace function missing_order
as
  missing_po_found integer;
begin
  select count(*)
  into   missing_po_found
  from   po_list p
  where  not exists (
           select 1
           from   orders o
           where  o.order_po = p.po)
  and    round = 1;

  return 1 - missing_o_found;
end;
没有运行,所以不能100%确定没有输入错误。

您可以使用减号:

您可以使用负号:


您希望执行的确切测试是什么?若要返回1,订单中必须至少存在一条记录,并且订单列表表中没有相应的订单值,是吗?我正在尝试确定订单列表表中是否有不在订单表中的订单。如果订单中不存在PO_LIST.PO,则返回0,否则返回1。您希望执行的确切测试是什么?若要返回1,订单中必须至少存在一条记录,并且订单列表表中没有相应的订单值,是吗?我正在尝试确定订单列表表中是否有不在订单表中的订单。如果订单中不存在PO_LIST.PO,则返回0,否则返回1。谢谢@David AldridgeThanks@David Aldridge