在Oracle中,SELECT INTO語句用于將查詢結果存儲到一個變量或表中。
具體來說,SELECT INTO語句可以用于以下幾個方面:
DECLARE
v_name employee.name%TYPE;
BEGIN
SELECT name INTO v_name FROM employee WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);
END;
DECLARE
v_name employee.name%TYPE;
v_salary employee.salary%TYPE;
BEGIN
SELECT name, salary INTO v_name, v_salary FROM employee WHERE id = 1;
DBMS_OUTPUT.PUT_LINE('Name: ' || v_name || ', Salary: ' || v_salary);
END;
CREATE TABLE new_employee AS
SELECT name, salary FROM employee WHERE hire_date > SYSDATE - 365;
需要注意的是,SELECT INTO語句只能用于返回單行查詢結果。如果查詢結果返回多行,則會拋出異常。如果需要處理多行結果,可以使用游標或BULK COLLECT INTO語句。