简体   繁体   中英

Select statement inside an Oracle stored procedure

I am trying to self-teach myself SQL. I am working on calling a simple select statement from a stored procedure in oracle.

I have created an employee database with 2 tables; employees and department. I want a select statement which returns all employees from a certain department.

This is what I have so far and I can't figure out where I am going wrong

create or replace procedure user_empdepart (depart_name varchar(40))
AS
BEGIN
    SELECT emp_name FROM employee JOIN department ON department.departmentID = 
employee.departmentID
    WHERE depart_name = 'research';
END;

And then I hope to call the above by;

exec user_empdepart(research);

I am using SQL Developer Oracle

I get the following error message:

Error(99,50): PLS-00103: Encountered the symbol "DEPARTMENT" when expecting one of the following: , ; for group having intersect minus order start union where connect

I think you need this -

create or replace procedure user_empdepart (depart_name varchar(40))
AS
DECLARE dept_name varchar(200);
dept_name := depart_name;
BEGIN
SELECT emp_name FROM employee JOIN department ON department.departmentID = employee.departmentID
WHERE depart_name := dept_name;
END;

In Oracle database, SQL statements executed directly in the client (like SQL-Developer or SQLPlus) are treated as plain, ordinary SQL.
But if you are going to use SQL commands in a procedure or a function, Oracle treats them as PL/SQL commands, not SQL.
For simple INSERT, DELETE and UPDATE commands the syntax in PL/SQL is the same as in SQL, however for SELECT the syntax is slightly different: PL/SQL SELECT INTO Statement

You must use SELECT expression-list INTO variables-list/record FROM ..... syntax.
You cannot use SELECT expression-list FROM ..... , this generates a syntax error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM