0

我对 Oracle 还是很陌生,我遇到了一个问题,我已经为此苦苦挣扎了几个小时。

样本:

 Create Table Accounts (Id number(10),Balance number(16,3), Status Varchar2(50),Owner_Id number(10));
 Create Table Transactions (Id number(10),Amount number(16,3), Trxn_date date, Account_Id number(10));
 Create Table Owner (Id number(10), Firstname varchar2(50),Lastname varchar2(50));

 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (1,1000,'OPEN',10);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (2,5000,'CLOSED',11);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (3,1000,'OPEN',12);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (4,5000,'CLOSED',13);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (5,1000,'OPEN',14);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (6,5000,'CLOSED',15);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (7,1000,'OPEN',16);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (8,5000,'CLOSED',17);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (9,1000,'OPEN',18);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (10,5000,'CLOSED',19);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (11,1000,'OPEN',20);
 Insert Into Accounts(Id,Balance,Status,Owner_Id) Values (12,5000,'CLOSED',21);

 Insert Into Owner(Id,Firstname,Lastname) Values (10,'John','TEST1');
 Insert Into Owner(Id,Firstname,Lastname) Values (11,'John','TEST2');
 Insert Into Owner(Id,Firstname,Lastname) Values (10,'John','TEST3');
 Insert Into Owner(Id,Firstname,Lastname) Values (11,'John','TEST4');
 Insert Into Owner(Id,Firstname,Lastname) Values (10,'John','TEST5');
 Insert Into Owner(Id,Firstname,Lastname) Values (11,'John','TEST6');
 Insert Into Owner(Id,Firstname,Lastname) Values (10,'John','TEST7');
 Insert Into Owner(Id,Firstname,Lastname) Values (11,'John','TEST8');
 Insert Into Owner(Id,Firstname,Lastname) Values (10,'John','TEST9');
 Insert Into Owner(Id,Firstname,Lastname) Values (11,'John','TEST10');
 Insert Into Owner(Id,Firstname,Lastname) Values (10,'John','TEST11');
 Insert Into Owner(Id,Firstname,Lastname) Values (11,'John','TEST12');

 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (1,10,'02-FEB-2015',5);
 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (2,10,'02-APR-2015',5);
 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (3,10,'02-JUN-2015',5);
 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (4,10,'02-AUG-2015',5);
 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (5,10,'02-FEB-2015',2);
 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (6,10,'02-APR-2015',2);
 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (7,10,'02-JUN-2015',2);
 Insert Into Transactions(Id,Amount,Trxn_Date,Account_Id) Values (8,10,'02-AUG-2015',2);     

数据检查:

   Select Unique(Account_Id) From Accounts A
 Inner Join Owner B on B.ID=A.OWNER_ID 
 Inner Join Transactions I on I.ACCOUNT_ID=A.Id
   Where I.Trxn_date Between '01-FEB-2015' and '01-JUL-2015' 
 And A.Status='CLOSED'
 and A.Balance=5000;/*1 Row Returned*/

循环必须在返回的第一个 Id 处退出

   Declare
   l_NewDate date:='01-FEB-2015';
   l_OldDate date:='01-JUL-2015';
   l_pID number(10);

 Begin
   For I in (Select Account_Id From Transactions 
   Where Trxn_date Between l_NewDate and l_OldDate)

   Loop

   Select Id Into l_pID From 
   (Select B.Id From Accounts A 
   Inner Join Owner B on A.Owner_Id = B.Id
   Where A.Status = 'CLOSED' And A.Balance = 5000 And A.Id=I.Account_Id)
   Where rownum < 2;
   dbms_output.put_line(l_pID);
   Exit;

   End Loop;
   End;

 ORA-01403: No data found

 ORA-06512: at line 12

我不明白为什么上面的数据检查清楚地表明没有找到数据。

问候 J.奥尔森

4

1 回答 1

1

就像你说的,你的数据检查查询:

Select Unique(Account_Id)
  From Accounts A
 Inner Join Owner B on B.ID=A.OWNER_ID 
 Inner Join Transactions I on I.ACCOUNT_ID=A.Id
 Where I.Trxn_date Between '01-FEB-2015' and '01-JUL-2015' 
   And A.Status='CLOSED'
   and A.Balance=5000;

...返回具有单个Account_Id值的单行2

但是,您的 PL/SQL 代码基本上将逻辑拆分为 2 个查询。您循环的查询是:

Select Account_Id
  From Transactions 
 Where Trxn_date Between '01-FEB-2015' and '01-JUL-2015'

而且,当我运行它时,它会返回:

5
5
5
2
2
2

现在上面的顺序不能保证,因为你没有ORDER BY条款。但是,如果您以与我相同的顺序获得结果,那么您的第一个循环迭代将使用5作为输入执行下一个查询:

Select *
  From Accounts A 
 Inner Join Owner B on A.Owner_Id = B.Id
 Where A.Status = 'CLOSED'
   And A.Balance = 5000
   And A.Id = 5

...它不返回任何数据,这就是你得到错误的原因。

如果您有幸从 的值开始2

Select *
  From Accounts A 
 Inner Join Owner B on A.Owner_Id = B.Id
 Where A.Status = 'CLOSED'
   And A.Balance = 5000
   And A.Id = 2

...它会按预期工作。

我希望我能推荐一个合适的解决方案,但我只是不明白你想要做什么。但确实感觉你不应该需要 PL/SQL 循环来做你想做的事。一个简单的查询就足够了。您的数据检查查询似乎是一个好的开始。

编辑

对于它的价值,我认为这是一种更直接的方式来做你打算做的完全相同的事情(没有循环):

Declare
   l_NewDate date:='01-FEB-2015';
   l_OldDate date:='01-JUL-2015';
   l_pID number(10);
 Begin
   select o.id into l_pID
    from transactions t
    join accounts a
      on a.id = t.account_id
     and a.status = 'CLOSED'
     and a.balance = 5000
    join owner o
      on o.id = a.owner_id
   where t.trxn_date between l_NewDate and l_OldDate
     and rownum < 2;

  dbms_output.put_line(l_pID);
End;
于 2015-10-02T23:01:55.380 回答