Oracle动态SQL绑定变量的方法有以下几种:
示例:
DECLARE
sql_stmt VARCHAR2(100);
emp_name VARCHAR2(50) := 'John';
BEGIN
sql_stmt := 'SELECT * FROM employees WHERE last_name = :name';
EXECUTE IMMEDIATE sql_stmt USING emp_name;
END;
示例:
DECLARE
cursor_id INTEGER;
emp_name VARCHAR2(50) := 'John';
BEGIN
cursor_id := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(cursor_id, 'SELECT * FROM employees WHERE last_name = :name', DBMS_SQL.NATIVE);
DBMS_SQL.BIND_VARIABLE(cursor_id, ':name', emp_name);
DBMS_SQL.EXECUTE(cursor_id);
-- Fetch rows and process results
DBMS_SQL.FETCH_ROWS(cursor_id);
-- Close cursor
DBMS_SQL.CLOSE_CURSOR(cursor_id);
END;
示例:
DECLARE
emp_name VARCHAR2(50) := 'John';
BEGIN
EXECUTE IMMEDIATE 'SELECT * FROM employees WHERE last_name = :name' USING emp_name;
END;
以上是Oracle动态SQL绑定变量的几种方法,具体选择哪种方法取决于需求和个人偏好。