我正在尝试通过两个条件从order_master表中检索记录。
select * from order_master
where ContractNo = 'D00101'
and Contract_SubCode = 'A';
我可以从Oracle SQL Developer中看到结果。但是我想通过提供变量值而不是硬编码值来检索记录。
所以我尝试以下方法。
Define Var_ContractNo = 'D09119';
Define Var_Contract_SubCode = 'A';
select * from order_master
where ContractNo = &Var_ContractNo
and Contract_SubCode = &Var_Contract_SubCode;
然后我收到以下错误。
ORA-00904: "A": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 6 Column: 24
所以我尝试了另一种方法。
var Var_ContractNo VARCHAR2(20);
var Var_Contract_SubCode VARCHAR(2);
BEGIN
select 'D09119', 'A' into :Var_ContractNo, :Var_Contract_SubCode from dual;
END;
select * from order_master
where ContractNo = :Var_ContractNo
and Contract_SubCode = :Var_Contract_SubCode;
然后我收到了其他错误消息
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | BLOB | BFILE
REFCURSOR | BINARY_FLOAT | BINARY_DOUBLE ] ]
Error starting at line : 3 in command -
BEGIN
select 'D09119', 'A' into :Var_ContractNo, :Var_Contract_SubCode from dual;
END;
select * from order_master
where ContractNo = :Var_ContractNo
and Contract_SubCode = :Var_Contract_SubCode;
Error report -
ORA-06550: line 5, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
请提供意见。谢谢。
您只需要按如下方式将替换变量包装在查询的single quotes
中:
Define Var_ContractNo = 'D09119';
Define Var_Contract_SubCode = 'A';
select * from order_master
where ContractNo = '&Var_ContractNo'
and Contract_SubCode = '&Var_Contract_SubCode';