Oracle SQL Code - sql

Trying to use this code, which was provided on here, however, it returns the following error when trying to run:
PLS-00103: Encountered the symbol "IS" when expecting one of the following:
constant exception table long double ref char time timestamp interval
date binary national character nchar
1. DECLARE
2. emp employee%ROWTYPE;
3. tbl_emp IS TABLE OF emp;
4. v_user_type employee.user_type%TYPE;
5. BEGIN
Code is:
DECLARE
emp employee%ROWTYPE;
tbl_emp IS TABLE OF emp;
v_user_type employee.user_type%TYPE;
BEGIN
SELECT user_type
INTO v_user_type
FROM Employee
WHERE upper(username) = v('APP_USER');
IF v_user_type = 1
THEN
SELECT * BULK COLLECT INTO tbl_emp
FROM employee;
ELSE
SELECT * BULK COLLECT INTO tbl_emp
FROM employee;
WHERE upper(username) = v('APP_USER');
END IF;
END;
/

You need to first declare type, then variable of that type.
E.g.
declare
type tbl_emp_type IS TABLE OF employee%ROWTYPE;
tbl_emp tbl_emp_type;
begin
select *
bulk collect into tbl_emp
from employee;
end;
SQL Fiddle

Related

How to start procedure in oracle?

So I have this SELECT statement:
SELECT emp.emp_name, pos_emp.POSITION_NAME
from EMPLOYEE emp
join POSITION_EMPLOYEE pos_emp
on emp.POSITION_EMPLOYEE_POSITION_ID=pos_emp.POSITION_ID
where emp.EMP_NAME='&employee_name';
When I enter employee name from keyboard it returns me his name(from EMPLOYEE table) and position(from POSITION_EMPLOYEE table). But I want to do this with stored procedure:
create or replace PROCEDURE emp_pos( EMPLOYEE_NAME IN EMPLOYEE.EMP_NAME%TYPE,
POSITION_NAME OUT POSITION_EMPLOYEE.POSITION_NAME%TYPE )
AS
BEGIN
SELECT pos_emp.POSITION_NAME
INTO
POSITION_NAME
FROM EMPLOYEE emp
JOIN
POSITION_EMPLOYEE pos_emp
ON
emp.POSITION_EMPLOYEE_POSITION_ID
= pos_emp.POSITION_ID
WHERE emp.EMP_NAME
= EMPLOYEE_NAME
;
END;
I am trying to start the procedure with begin:
begin emp_pos('&employee_name');
end;
The compiler gives me error:wrong number or types of arguments in call to 'EMP_POS'. Where am I wrong?
SQL>set serveroutput on;
SQL> create or replace PROCEDURE emp_pos( EMPLOYEE_NAME IN EMPLOYEE.EMP_NAME%TYPE,
POSITION_NAME OUT POSITION_EMPLOYEE.POSITION_NAME%TYPE )
AS
BEGIN
SELECT pos_emp.POSITION_NAME
INTO
POSITION_NAME
FROM EMPLOYEE emp
JOIN
POSITION_EMPLOYEE pos_emp
ON
emp.POSITION_EMPLOYEE_POSITION_ID
= pos_emp.POSITION_ID
WHERE emp.EMP_NAME
= EMPLOYEE_NAME
;
dbms_output.put_line(EMPLOYEE_NAME||'''s position is : '||POSITION_NAME);
END;
after defining this procedure, you may use in this way :
SQL>var v_emp_pos varchar2;
SQL>exec emp_pos('&employee_name',:v_emp_pos);
SQL>print v_emp_pos; -- just to print out this "out" parameter.

PL/SQL take input as table

DECLARE
tablename table;
nor number(10);
BEGIN
tablename:=&tablename;
select count(*) into nor from tablename;
dbms_output.put_line('The number of rows are '||nor);
END;
/
I am using this code to take table name as input from user and displaying the row count but it shows errors but if use a specific table name it runs fine!
DECLARE
tablename table;
nor number(10);
BEGIN
tablename:=&tablename;
execute immediate 'select count(*) from '|| tablename
into nor;
dbms_output.put_line('The number of rows are '||nor);
END;
/
You can try to use EXECUTE IMMDEDIATE

Error when creating cursor in Oracle

I have a very simple script to try the cursor function but it reported invalid CREATE COMMAND. Anyone know what could be the cause? Thank you!
create lot_1 cursor for select * from temp_minj1.lot
In Oracle, the SQL syntax is thus :
DECLARE
my_emp_id NUMBER(6); -- variable for employee_id
my_job_id VARCHAR2(10); -- variable for job_id
my_sal NUMBER(8,2); -- variable for salary
CURSOR c1 IS SELECT employee_id, job_id, salary FROM employees
WHERE salary > 2000;
my_dept departments%ROWTYPE; -- variable for departments row
CURSOR c2 RETURN departments%ROWTYPE IS
SELECT * FROM departments WHERE department_id = 110;
see source

pl/sql function- pass multiple varchar2 values dynamically for SELECT ... NOT IN()

I have a table EMP with following definition:
EMP_ID NOT NULL NUMBER(6)
EMP_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
If we want to count employees whose name not in 'King', 'Steve', 'John' we simply use this query :
SELECT count(*) FROM emp WHERE emp_name NOT IN('King','Steve','John');
Now Here is what I want above this:
What I want to do is, Create a PL/SQL Function which returns number of count according to the dynamic input, Like if we pass:
SELECT count_emp('King,Steve,John') FROM dual;
SELECT count_emp('William,Donald') FROM dual;
SELECT count_emp('Daniel') FROM dual;
needs to return appropriate count, how can I achieve this using PL/SQL FUNCTION
This is what I have tried and Needs guideline:
CREATE OR REPLACE FUNCTION count_emp(emp_nm IN varchar)
RETURN number
IS
cnt NUMBER;
BEGIN
SELECT count(*) INTO cnt FROM emp WHERE emp_name NOT IN(emp_nm);
RETURN cnt;
END;
it is giving result for single name, but how can I split/format multiple input(i.e. emp_nm) to pass in NOT IN()?
Try like this,
CREATE OR REPLACE
FUNCTION count_emp(emp_nm IN VARCHAR)
RETURN NUMBER
IS
cnt NUMBER;
BEGIN
SELECT count(*)
INTO cnt
FROM emp
WHERE ename NOT IN(
SELECT regexp_substr (emp_nm, '[^,]+', 1, ROWNUM)
FROM dual
CONNECT BY LEVEL <= LENGTH (regexp_replace (emp_nm, '[^,]+')) + 1);
RETURN cnt;
END;
You can try dynamic sql:
CREATE OR REPLACE FUNCTION count_emp(emp_nm IN varchar)
RETURN number
IS
cnt NUMBER;
BEGIN
Execute immediate 'SELECT count(*) FROM emp WHERE emp_name NOT IN(' || emp_nm || ')' returning into cnt;
RETURN cnt;
END;
You can also use MEMBER OF. Here is a snippet. Hope this helps!
-- Create a type in SQL
CREATE OR REPLACE TYPE t_emp_name AS TABLE OF VARCHAR2 (10);
-- use MEMBER OF to use your list as IN parameter
CREATE OR REPLACE FUNCTION count_emp (emp_nm IN t_emp_name)
RETURN NUMBER
IS
cnt NUMBER;
BEGIN
SELECT COUNT (*)
INTO cnt
FROM emp
WHERE emp_name NOT MEMBER OF (emp_nm);
RETURN cnt;
END;
-- Assign values to the list, you can do it dynamically as well. Call the function
DECLARE
l_emp_name_list t_emp_name;
lv_count NUMBER;
BEGIN
l_emp_name_list := t_emp_name ('King', 'Steve'); --add more names as needed
lv_count := count_emp (l_emp_name_list);
END;

Oracle: Return multiple values in a function

I'm trying to return a multiple values in a %rowtype from a function using two table(employees and departments), but it not working for me.
create or replace function get_employee
(loc in number)
return mv_emp%rowtype
as
emp_record mv_emp%rowtype;
begin
select a.first_name, a.last_name, b.department_name into emp_record
from employees a, departments b
where a.department_id=b.department_id and location_id=loc;
return(emp_record);
end;
The above function compiled without any error? What is the type of MV_EMP? Ideally, it should be something like below.
create or replace type emp_type
(
first_name varchar2(20)
, last_name varchar2(20)
, depart_name varchar2(20)
)
/
create or replace function get_employee
(loc in number)
return emp_type
as
emp_record emp_type;
begin
select a.first_name, a.last_name, b.department_name into emp_record
from employees a, departments b
where a.department_id=b.department_id and location_id=loc;
return(emp_record);
end;
create type t_row as object (a varchar2(10));
create type t_row_tab as table of t_row;
We will now create a function which will split the input string.
create or replace function get_number(pv_no_list in varchar2) return t_row_tab is
lv_no_list t_row_tab := t_row_tab();
begin
for i in (SELECT distinct REGEXP_SUBSTR(pv_no_list, '[^,]+', 1, LEVEL) no_list FROM dual
CONNECT BY REGEXP_SUBSTR(pv_no_list, '[^,]+', 1, LEVEL) IS NOT NULL)
loop
lv_no_list.extend;
lv_no_list(lv_no_list.last) := t_row(i.no_list);
end loop;
return lv_no_list;
end get_number;
Once the function is in place we can use the table clause of sql statement to get the desired result. As desired we got multiple values returned from the function.
SQL> select * from table(get_number('1,2,3,4'));
A
----------
1
3
2
4
So now our function is simply behaving like a table. There can be a situation where you want these comma separated values to be a part of "IN" clause.
For example :
select * from dummy_table where dummy_column in ('1,2,3,4');
But the above query will not work as '1,2,3,4' is a string and not individual numbers. To solve this problem you can simply use following query.
select * from dummy_table where dummy_column in ( select * from table(get_number('1,2,3,4')) );
References : http://www.oraclebin.com/2012/12/returning-multiple-values-from-function.html
CREATE OR replace FUNCTION Funmultiple(deptno_in IN NUMBER)
RETURN NUMBER AS v_refcursur SYS_REFCURSOR;
BEGIN
OPEN v_refcursor FOR
SELECT *
FROM emp
WHERE deptno = deptno_in;
retun v_refcursor;
END;
To call it, use:
variable x number
exec :x := FunMultiple(10);
print x