Oracle動(dòng)態(tài)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動(dòng)態(tài)SQL綁定變量的幾種方法,具體選擇哪種方法取決于需求和個(gè)人偏好。