Oracle SQL Developer-在插入/选择中使用变量

时间:2019-04-10 20:41:49

标签: plsql oracle11g oracle-sqldeveloper

使用Oracle SQL Developer。我有以下示例工作:

DECLARE
    v_next_TransId INTEGER; -- declare
BEGIN
    select (max(Transaction_ID)+1) into v_next_TransId from xxpos.pos_transactions;
    dbms_output.Put_line(v_next_TransId); --display

END;

现在,我想在插入和/或选择中使用该变量,我尝试使用和不使用:前缀。以下示例为我提供了ORA-01008:找不到所有变量。

DECLARE
    v_next_TransId INTEGER; -- declare 
BEGIN
    select (max(Transaction_ID)+1) into v_next_TransId from xxpos.pos_transactions;
    dbms_output.Put_line(v_next_TransId); --display

    -- insert new row will go here 
    -- commit will go here 
    -- now verify the insert worked okay 
    --select * from xxpos.pos_transactions where Transaction_ID = ( select max(Transaction_ID) from xxpos.pos_transactions )
    select * from xxpos.pos_transactions where Transaction_ID = :v_next_TransId;

END;

在变量前面没有:时,出现以下语法错误: enter image description here

版本:

Oracle数据库11g企业版11.2.0.4.0版-64位生产

PL / SQL版本11.2.0.4.0-生产

1 个答案:

答案 0 :(得分:0)

在PLSQL代码块中,必须将select语句的列分配给变量。您必须在plsql代码块中使用select into子句,如下所示。

DECLARE
    v_next_TransId INTEGER; -- declare 
    l_pos_transactions xxpos.pos_transactions%ROWTYPE;
BEGIN
    select (max(Transaction_ID)+1) into v_next_TransId from xxpos.pos_transactions;
    dbms_output.Put_line(v_next_TransId); --display

    -- insert new row will go here 
    -- commit will go here 
    -- now verify the insert worked okay 
    --select * from xxpos.pos_transactions where Transaction_ID = ( select max(Transaction_ID) from xxpos.pos_transactions )
    select * into l_pos_transactions
from xxpos.pos_transactions where Transaction_ID = :v_next_TransId;

  DBMS_OUTPUT.PUT_LINE(l_pos_transactions.column1|| ',' || l_pos_transactions.column2);

END;