The stored procedures being written here currently concats the parameters to the queries:
此处编写的存储过程当前将参数连接到查询:
SELECT *
FROM Names
WHERE Name = ' || prmName || '
ORDER BY ' || prmSortField
Is it possible to parameterize this query inside the stored procedure? Possibly like:
是否可以在存储过程中参数化此查询?可能像:
query = 'select * From Names Where Name = @name Order By ' || prmSortField
call(query, prmName)
Note:
In case you wonder why we do so, there are two common parameters for our sp's: sortFieldIndex
and sortDirection
. Since we cannot directly parameterize these, the query is dynamically generated. But other parameters make the queries open for injection. So I am looking a way to parameterize some of the parameters.
注意:如果您想知道我们为什么这样做,我们的sp有两个常见参数:sortFieldIndex和sortDirection。由于我们无法直接参数化这些,因此动态生成查询。但是其他参数使查询打开以进行注入。所以我正在寻找一种参数化一些参数的方法。
3 个解决方案
#1
Absolutely. Use cursors.
绝对。使用游标。
DECLARE
CURSOR c1 (job VARCHAR2, max_wage NUMBER) IS
SELECT * FROM employees WHERE job_id = job AND salary > max_wage;
BEGIN
FOR person IN c1('CLERK', 3000)
LOOP
-- process data record
DBMS_OUTPUT.PUT_LINE('Name = ' || person.last_name || ', salary = ' ||
person.salary || ', Job Id = ' || person.job_id );
END LOOP;
END;
#2
For a dynamic query with bind values, do this:
对于具有绑定值的动态查询,请执行以下操作:
procedure p (prmName varchar2, prmSortField varchar2)
is
query varchar2(100);
rc sys_refcursor;
names_rec names%rowtype;
begin
query = 'select * From Names Where Name = :name Order By ' || prmSortField
open rc for query using prmName;
loop
fetch rc into names_rec;
exit when rc%notfound;
-- process this row
end loop;
close rc;
end;
#3
For a more elaborate procedure that supports optional parameter values (but uses sys context), check out the following post on Asktom.com
有关支持可选参数值的更复杂的过程(但使用sys上下文),请查看Asktom.com上的以下帖子
PRATTY -- Thanks for the question regarding 'CURSOR'...
PRATTY - 感谢关于'CURSOR'的问题......
#1
Absolutely. Use cursors.
绝对。使用游标。
DECLARE
CURSOR c1 (job VARCHAR2, max_wage NUMBER) IS
SELECT * FROM employees WHERE job_id = job AND salary > max_wage;
BEGIN
FOR person IN c1('CLERK', 3000)
LOOP
-- process data record
DBMS_OUTPUT.PUT_LINE('Name = ' || person.last_name || ', salary = ' ||
person.salary || ', Job Id = ' || person.job_id );
END LOOP;
END;
#2
For a dynamic query with bind values, do this:
对于具有绑定值的动态查询,请执行以下操作:
procedure p (prmName varchar2, prmSortField varchar2)
is
query varchar2(100);
rc sys_refcursor;
names_rec names%rowtype;
begin
query = 'select * From Names Where Name = :name Order By ' || prmSortField
open rc for query using prmName;
loop
fetch rc into names_rec;
exit when rc%notfound;
-- process this row
end loop;
close rc;
end;
#3
For a more elaborate procedure that supports optional parameter values (but uses sys context), check out the following post on Asktom.com
有关支持可选参数值的更复杂的过程(但使用sys上下文),请查看Asktom.com上的以下帖子
PRATTY -- Thanks for the question regarding 'CURSOR'...
PRATTY - 感谢关于'CURSOR'的问题......