[英]Dynamic SQL | PL/SQL
I've run into trouble with Dynamic SQL, I want to be able to display the department_id: 我在使用动态SQL时遇到了麻烦,我希望能够显示department_id:
CREATE OR REPLACE PROCEDURE pro_two_tables
(p_input VARCHAR2) IS
v_department_id employees.department_id%TYPE;
BEGIN
EXECUTE IMMEDIATE
'SELECT department_id
/* MYSTERY FOR ME */
FROM ' || p_input || '
WHERE manager_id = 205';
DBMS_OUTPUT.PUT_LINE('You selected ID from the table' || ' ' || p_input || ' ' || 'ID is' ||' ' || v_department_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(' I petty the fool who wrote this');
END;
I seem to be unable to understand how would one get to use the "INTO" clause in this procedure. 我似乎无法理解在此过程中如何使用“ INTO”子句。 I'm reading up on the issue and doing some "tests" although I believe it to be great help if someone could explain this to me.
我正在阅读有关此问题的内容并进行一些“测试”,尽管我相信如果有人可以向我解释这一点将对您有很大帮助。
The INTO goes behind the execute immediate as in INTO跟在立即执行之后
declare
foo number;
bar number;
begin
execute immediate 'select 1,2 from dual' into foo, bar;
dbms_output.put_line(foo ||','||bar);
end;
/
This prints: 打印:
1,2
Note that if you find youeself using dynamic SQL, it is often a good idea to review your design. 请注意,如果您使用动态SQL找到自己,那么通常回顾一下设计是一个好主意。
CREATE OR REPLACE PROCEDURE pro_two_tables (i_table_name VARCHAR2)
IS
v_record_id NUMBER;
v_record_name VARCHAR2 (500) := 'Name Record'; -- Insert your Name Value
sql_stmt VARCHAR2 (500);
BEGIN
sql_stmt :=
'SELECT id
FROM ' || i_table_name || ' WHERE name = :1';
EXECUTE IMMEDIATE sql_stmt INTO v_record_id USING v_record_name;
DBMS_OUTPUT.PUT_LINE( 'You selected ID from the table'
|| ' '
|| i_table_name
|| ' '
|| 'ID is'
|| ' '
|| v_record_id);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.PUT_LINE ('No data found');
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE (' I petty the fool who wrote this');
END;
With EXECUTE IMMEDIATE you can USE: 使用立即执行,您可以使用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.