参数赋值错误(PL/SQL, ORACLE)
Parameters wrong assignment (PL/SQL, ORACLE)
我正尝试 运行 在 pl/sql 块下方,但出现错误:
set serveroutput on
declare
rowBefore VARCHAR2(32000);
myschema VARCHAR2(32000) := 'abc';
mytable VARCHAR2(32000) := 'table1';
param1 VARCHAR2(32000) := 'Tom';
begin
select count(*) into rowBefore from myschema.mytable where colA = param1;
--select count(*) into rowBefore from abc.table1 where colA = 'Tom';
DBMS_OUTPUT.PUT_LINE(rowBefore);
End;
如何在 select 语句中正确使用我的所有参数?
更新错误:
Error report -
ORA-06550: linia 7, kolumna 50:
PL/SQL: ORA-00942: tabela lub perspektywa nie istnieje
ORA-06550: linia 7, kolumna 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
您不能在 SQL 语句中使用变量作为标识符。它正在寻找一个实际上称为 mytable
的 table - 而不是像您期望的那样具有具有该名称的变量值的变量。架构也是如此。
If the statement is a SELECT statement, the PL/SQL compiler removes the INTO clause.
The PL/SQL compiler sends the statement to the SQL subsystem.
The SQL subsystem checks the syntax of the statement.
If the syntax is incorrect, the compilation of the PL/SQL unit fails. If the syntax is correct, the SQL subsystem determines the names of the tables and tries to resolve the other names in the scope of the SQL statement.
...
它尝试解析 其他 个名称,例如职能;但是 table 名称(以及架构名称和列名称)必须对 SQL 解析器有效。
您必须使用动态 SQL 创建一个包含语句的字符串,并在标识符中连接:
execute immediate 'select count(*) from ' || myschema ||'.'|| mytable
|| ' where colA = :value'
into rowBefore using param1;
如果您使用 dbms_output
打印生成的语句,您应该会看到它与注释掉的版本匹配,除了对列值使用绑定变量。这也有助于突出显示任何错误 - 例如在语句的连接部分之间不包含空格,这是一个常见且容易犯的错误。
将语句构建为字符串变量通常是个好主意,这样调试起来更容易:
declare
...
v_stmt varchar2(4000):
begin
v_stmt := 'select count(*) from ' || myschema ||'.'|| mytable
|| ' where colA = :value';
-- for debugging
dbms_output.put_line(v_stmt);
-- and run it
execute immediate vStmt into rowBefore using param1;
...
如果您确实获得了用户或客户端传入的标识符,您应该验证它们以避免意外错误和SQL injection attacks。此外,如果标识符可能区分大小写(即 schemas/tables 使用带引号的标识符创建),那么您可能需要在生成的语句中添加双引号,并确保变量大小写正确。
错误是 ORA-942: table 或视图不存在。 运行先在下面查询,看是否还报错。
-- should you use abc.table1, instead of myschema.mytable?
select count(*) rowBefore from myschema.mytable where colA = 'a';
如果仍然存在错误,则在 PL/SQL 块中使用它之前先修复 SELECT 语句。
我正尝试 运行 在 pl/sql 块下方,但出现错误:
set serveroutput on
declare
rowBefore VARCHAR2(32000);
myschema VARCHAR2(32000) := 'abc';
mytable VARCHAR2(32000) := 'table1';
param1 VARCHAR2(32000) := 'Tom';
begin
select count(*) into rowBefore from myschema.mytable where colA = param1;
--select count(*) into rowBefore from abc.table1 where colA = 'Tom';
DBMS_OUTPUT.PUT_LINE(rowBefore);
End;
如何在 select 语句中正确使用我的所有参数?
更新错误:
Error report -
ORA-06550: linia 7, kolumna 50:
PL/SQL: ORA-00942: tabela lub perspektywa nie istnieje
ORA-06550: linia 7, kolumna 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
您不能在 SQL 语句中使用变量作为标识符。它正在寻找一个实际上称为 mytable
的 table - 而不是像您期望的那样具有具有该名称的变量值的变量。架构也是如此。
If the statement is a SELECT statement, the PL/SQL compiler removes the INTO clause.
The PL/SQL compiler sends the statement to the SQL subsystem.
The SQL subsystem checks the syntax of the statement.
If the syntax is incorrect, the compilation of the PL/SQL unit fails. If the syntax is correct, the SQL subsystem determines the names of the tables and tries to resolve the other names in the scope of the SQL statement.
...
它尝试解析 其他 个名称,例如职能;但是 table 名称(以及架构名称和列名称)必须对 SQL 解析器有效。
您必须使用动态 SQL 创建一个包含语句的字符串,并在标识符中连接:
execute immediate 'select count(*) from ' || myschema ||'.'|| mytable
|| ' where colA = :value'
into rowBefore using param1;
如果您使用 dbms_output
打印生成的语句,您应该会看到它与注释掉的版本匹配,除了对列值使用绑定变量。这也有助于突出显示任何错误 - 例如在语句的连接部分之间不包含空格,这是一个常见且容易犯的错误。
将语句构建为字符串变量通常是个好主意,这样调试起来更容易:
declare
...
v_stmt varchar2(4000):
begin
v_stmt := 'select count(*) from ' || myschema ||'.'|| mytable
|| ' where colA = :value';
-- for debugging
dbms_output.put_line(v_stmt);
-- and run it
execute immediate vStmt into rowBefore using param1;
...
如果您确实获得了用户或客户端传入的标识符,您应该验证它们以避免意外错误和SQL injection attacks。此外,如果标识符可能区分大小写(即 schemas/tables 使用带引号的标识符创建),那么您可能需要在生成的语句中添加双引号,并确保变量大小写正确。
错误是 ORA-942: table 或视图不存在。 运行先在下面查询,看是否还报错。
-- should you use abc.table1, instead of myschema.mytable?
select count(*) rowBefore from myschema.mytable where colA = 'a';
如果仍然存在错误,则在 PL/SQL 块中使用它之前先修复 SELECT 语句。