【发布时间】:2018-05-22 21:23:44
【问题描述】:
我正在尝试按如下方式创建存储过程:
CREATE OR REPLACE PROCEDURE storedprocedure(emp number) AS
BEGIN
DECLARE
-- create the cursor based on a query
cursor emp_cursor is
select e.employeeid, firstname, lastname, e.departmentid, e.title,
salary, d.departmentname, billrate
from employees e
full join departments d on e.departmentID = d.departmentID
full join employeeproject p on e.employeeID = p.employeeID where e.employeeID = emp;
BEGIN
open emp_cursor;
-- first display information about the employee
dbms_output.put_line('- -');
dbms_output.put_line('- -');
dbms_output.put_line('Employee#' || e.employeeid
|| ' Name:' || TRIM(e.firstname) || ' ' || TRIM(e.lastname)
|| ' Dept: ');
dbms_output.put_line('_________________________________________________________');
dbms_output.put_line('- -');
dbms_output.put_line('- - Title: ' || e.title
|| ' Salary: ' || to_char(e.salary,'$999,999,999.99'));
dbms_output.put_line('- - Billing Rate: ' || to_char(billrate,'$999,999.99'));
-- next call the stored procedure to show department information
END;
END;
/
但它编译时出错。当我show errors 它告诉我e.employeeID、e.title 和billrate 都必须声明,但它们是原始查询。我在这里做错了什么?我是否误解了声明它们的含义?
这些是在被查询的表中存在的所有列,并在 SQL 获取结果时运行查询。
【问题讨论】:
-
请标记您正在使用的特定 SQL 产品 - SQL Server、Oracle、MySQL、PostgreSQL 等。
-
会的,我用的是oracle。我现在就加标签
-
使用格式有点难以阅读,但您需要某种循环构造和 fetch 以循环游标。请记住,在 SQL 数据库中,游标通常应该是最后的手段。通常最好使用基于集合的方法。
标签: sql oracle stored-procedures cursor declare