Plsql 精确获取返回的行数超过请求的行数PL/SQL

Plsql 精确获取返回的行数超过请求的行数PL/SQL,plsql,Plsql,这是我尝试的代码 SET SERVEROUTPUT ON; ACCEPT input_accountNumber NUMBER PROMPT 'Enter the account number : ' ACCEPT input_branch CHAR PROMPT 'Enter the branch : ' CREATE FUNCTION activeAccounts RETURN NUMBER IS accountNumber NUMBER; BEGIN FOR rec IN

这是我尝试的代码

SET SERVEROUTPUT ON;
ACCEPT input_accountNumber NUMBER PROMPT 'Enter the account number : '
ACCEPT input_branch CHAR PROMPT 'Enter the branch : '

CREATE FUNCTION activeAccounts
RETURN NUMBER    IS
  accountNumber NUMBER;
BEGIN
  FOR rec IN (SELECT account_number, branch FROM ACCOUNT_DATA WHERE status='Active') 
  LOOP
    INSERT INTO ACTIVE_ACCOUNTS VALUES (rec.account_number,rec.branch);
  END LOOP;
END;
/

DECLARE
  accountStatus VARCHAR(20);
  inputuser_accountNumber NUMBER;
  inputuser_branch VARCHAR(20);
  cf varchar(20);
BEGIN
  inputuser_accountNumber := '&input_accountNumber';
  inputuser_branch := '&input_branch';
  SELECT status INTO accountStatus FROM ACCOUNT_DATA;
  IF (accountStatus = 'Active') THEN
    cf := activeAccounts();
  ELSE
    DBMS_OUTPUT.PUT_LINE('The account is Inactive.');
  END IF;
END;
/
问题陈述是

在PL/SQL中为给定的需求编写一个存储函数,并在PL/SQL中使用相同的函数 块 用户将接受账号和分行名称。将在中搜索相同的内容 表acct\u详细信息。如果帐户状态为“活动”,则显示相应的消息,并且 将账户详情存储在活动账户详情表中,否则在屏幕上显示消息 “帐户处于非活动状态”

并且错误为

错误报告-ORA-01422:精确提取返回的值超过请求的值 行数

ORA-06512:在第9行

  • 00000-“精确获取返回的行数超过请求的行数” *原因:精确提取中指定的行数小于返回的行数。 *操作:重写查询或更改请求的行数

  • 代码中几乎没有可以纠正的缺陷,代码也可以正常工作。首先,根据您的问题陈述:

    用户将接受账号和分行名称。同样的意志 可以在表acct\U details中搜索

    这意味着您的
    Select
    查询必须具有一些过滤条件,以选择用户输入的唯一记录

    此外,代码中不需要函数,而更适合使用过程。您需要以下内容:

    --Passing the record to proc to store records to details table.
    CREATE OR REPLACE PROCEDURE activeaccounts (v_acct_num number, v_brnch varchar2)
    AS
    BEGIN
        FOR rec IN (
            SELECT
                account_number,
                branch
            FROM
                account_data
            WHERE
                status = 'Active'
           and  acct_number =   v_acct_num
           And   branch = v_brnch)
    
        ) LOOP
            INSERT INTO active_accounts VALUES (
                rec.account_number,
                rec.branch
            );
    
        END LOOP;
    END;
    /
    
    --Anonymous Block 
    DECLARE
        accountstatus             VARCHAR(20);
        inputuser_accountnumber   NUMBER;
        inputuser_branch          VARCHAR(20);
        cf                        VARCHAR(20);
    BEGIN
        inputuser_accountnumber := '&input_accountNumber';
        inputuser_branch        := '&input_branch';
    
        --As per your problem statement, your select statement must have account number and branch in where clause to pick one unique record.
        SELECT
            status
        INTO
            accountstatus
        FROM
            account_data
        Where acct_number =   inputuser_accountnumber;
        And   branch = inputuser_branch;
    
        IF
            ( accountstatus = 'Active' )
        THEN
           --Calling Proc to save records
           activeaccounts (inputuser_accountnumber,inputuser_branch);
        ELSE
            dbms_output.put_line('The account is Inactive.');
        END IF;
    
    END;
    /
    

    代码中几乎没有可以纠正的缺陷,代码也可以正常工作。首先,根据您的问题陈述:

    用户将接受账号和分行名称。同样的意志 可以在表acct\U details中搜索

    这意味着您的
    Select
    查询必须具有一些过滤条件,以选择用户输入的唯一记录

    此外,代码中不需要函数,而更适合使用过程。您需要以下内容:

    --Passing the record to proc to store records to details table.
    CREATE OR REPLACE PROCEDURE activeaccounts (v_acct_num number, v_brnch varchar2)
    AS
    BEGIN
        FOR rec IN (
            SELECT
                account_number,
                branch
            FROM
                account_data
            WHERE
                status = 'Active'
           and  acct_number =   v_acct_num
           And   branch = v_brnch)
    
        ) LOOP
            INSERT INTO active_accounts VALUES (
                rec.account_number,
                rec.branch
            );
    
        END LOOP;
    END;
    /
    
    --Anonymous Block 
    DECLARE
        accountstatus             VARCHAR(20);
        inputuser_accountnumber   NUMBER;
        inputuser_branch          VARCHAR(20);
        cf                        VARCHAR(20);
    BEGIN
        inputuser_accountnumber := '&input_accountNumber';
        inputuser_branch        := '&input_branch';
    
        --As per your problem statement, your select statement must have account number and branch in where clause to pick one unique record.
        SELECT
            status
        INTO
            accountstatus
        FROM
            account_data
        Where acct_number =   inputuser_accountnumber;
        And   branch = inputuser_branch;
    
        IF
            ( accountstatus = 'Active' )
        THEN
           --Calling Proc to save records
           activeaccounts (inputuser_accountnumber,inputuser_branch);
        ELSE
            dbms_output.put_line('The account is Inactive.');
        END IF;
    
    END;
    /
    

    函数意味着返回某些内容,而您需要执行某些操作,而不返回任何内容,因此我将解释该需求 如“构建存储过程”

    现在,假设你有你的程序,它只需要寻找一些特定的帐户分支和编号,所以它需要一些输入参数。 然后,此过程应检查表中的(唯一?)行,以获取帐户的状态(根据分行和账号的值,使用
    WHERE
    条件进行选择)。 一旦知道状态,该过程只需打印消息或进行插入即可

    有如下表格

    create table ACCOUNT_DATA(account_number, branch, status) as (
        select 1, 'x', 'Active'   from dual union all
        select 2, 'x', 'Inactive' from dual 
    )   
    
    create table active_accounts (account_number number, branch varchar2(10))
    
    您可以创建如下过程:

    create or replace procedure checkAccount(p_acc_number IN number, p_acc_branch IN varchar2) is
        v_status varchar2(10);
    begin
        -- get the status, assuming that the couple (account_number, and branch) is a key for the table 
        select status
        into v_status
        from ACCOUNT_DATA
        where account_number = p_acc_number
          and branch = p_acc_branch;
        -- check the status
        if v_status = 'Active' then
            insert into active_accounts
            values (p_acc_number, p_acc_branch);
        else
           dbms_output.put_line('The account is Inactive.');
        end if;
    end;     
    
    您的脚本可以是(test.sql):

    工作原理:

    SQL> select * from active_accounts;
    
    no rows selected
    
    SQL> sta d:\temp\test.sql
    Enter the account number : 1
    Enter the branch : x
    
    PL/SQL procedure successfully completed.
    
    SQL> sta d:\temp\test.sql
    Enter the account number : 2
    Enter the branch : x
    The account is Inactive.
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from active_accounts;
    
    ACCOUNT_NUMBER BRANCH
    -------------- ----------
                 1 x
    
    SQL>
    

    函数意味着返回某些内容,而您需要执行某些操作,而不返回任何内容,因此我将解释该需求 如“构建存储过程”

    现在,假设您有您的过程,它只需要查找特定的帐户分支和编号,因此需要一些输入参数。 然后,此过程应检查表中的(唯一?)行,以获取帐户的状态(根据分行和账号的值,使用
    WHERE
    条件进行选择)。 一旦知道状态,该过程只需打印消息或进行插入即可

    有如下表格

    create table ACCOUNT_DATA(account_number, branch, status) as (
        select 1, 'x', 'Active'   from dual union all
        select 2, 'x', 'Inactive' from dual 
    )   
    
    create table active_accounts (account_number number, branch varchar2(10))
    
    您可以创建如下过程:

    create or replace procedure checkAccount(p_acc_number IN number, p_acc_branch IN varchar2) is
        v_status varchar2(10);
    begin
        -- get the status, assuming that the couple (account_number, and branch) is a key for the table 
        select status
        into v_status
        from ACCOUNT_DATA
        where account_number = p_acc_number
          and branch = p_acc_branch;
        -- check the status
        if v_status = 'Active' then
            insert into active_accounts
            values (p_acc_number, p_acc_branch);
        else
           dbms_output.put_line('The account is Inactive.');
        end if;
    end;     
    
    您的脚本可以是(test.sql):

    工作原理:

    SQL> select * from active_accounts;
    
    no rows selected
    
    SQL> sta d:\temp\test.sql
    Enter the account number : 1
    Enter the branch : x
    
    PL/SQL procedure successfully completed.
    
    SQL> sta d:\temp\test.sql
    Enter the account number : 2
    Enter the branch : x
    The account is Inactive.
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from active_accounts;
    
    ACCOUNT_NUMBER BRANCH
    -------------- ----------
                 1 x
    
    SQL>
    

    您的帐户数据表可能有多行。因此,您不能在varchar类型的单个变量中选择更多值。您应该选择进入数组或只选择一个值,因为缺少
    return
    语句,所以函数不会
    返回任何内容。如果您只是想插入记录,则可以使用
    过程
    。您的帐户数据表可能不止一行。因此,您不能在varchar类型的单个变量中选择更多值。您应该选择进入数组或只选择一个值,因为缺少
    return
    语句,所以函数不会
    返回任何内容。如果您只是想插入记录,可以使用
    过程