pl-sql errror on select HR schema - sql

i want to do something like that.. but in a function.. select * from employees where employee_id = &employee_id in HR schema of oracle
Q: Create Function “SEARCH_EMPLOYEE” that receives an Employee ID and returns all its attributes through an output parameter with a data structure that represents all its information
R:
Create or replace FUNCTION get_complete_employee (&in_employee_id IN NUMBER)
AS person_details;
BEGIN
SELECT * --'Name-'||first_name||' '|| last_name
into person_details
FROM employees;
Dbms_output.put_line(person_details);
-- END get_complete_employee;
end;
i have a error of sintax i guess..
i don't know what is wrong

If you are learning how to create the function which returns entire row of the table then following is the basic example of doing the same:
Table data:
SQL> select * from EMP;
EMP_ID EMP_NAME E
---------- -------------------- -
10 John N
20 May Y
SQL>
Function:
SQL> CREATE OR REPLACE FUNCTION GET_COMPLETE_EMPLOYEE (
2 IN_EMPLOYEE_ID IN NUMBER
3 ) RETURN EMP%ROWTYPE AS
4 MY_ROWTYPE EMP%ROWTYPE;
5 BEGIN
6 SELECT
7 * --'Name-'||first_name||' '|| last_name
8 INTO MY_ROWTYPE
9 FROM
10 EMP
11 WHERE
12 EMP_ID = IN_EMPLOYEE_ID;
13
14 RETURN MY_ROWTYPE;
15 END;
16 /
Function created.
SQL>
Calling the function and result:
SQL> SET SERVEROUT ON
SQL>
SQL> DECLARE
2 EMPTYPE EMP%ROWTYPE;
3 BEGIN
4 EMPTYPE := GET_COMPLETE_EMPLOYEE(10);
5 DBMS_OUTPUT.PUT_LINE(EMPTYPE.EMP_NAME);
6 END;
7 /
John
You must have to handle multiple exception scenarios in real-world coding.
Cheers!!

Related

Compilation error in Stored Procedure (Oracle SQL)

create or replace function getAvg(id1 IN number, id2 IN number) return number as
sal1 number;
sal2 number;
avg number;
BEGIN
select esal into sal1 from employees where eno = id1;
select esal into sal2 from employees where eno = id2;
avg := (sal1+sal2)/2;
return avg;
END;
/
When I try to compile the above code, I get compilation errors with following message:
Warning: Function created with compilation errors.
But when I replace avg after return with (sal1+sal2)/2 it compiles successfully.
That's bad habits: never name your own objects, variables, whatever using reserved words or keywords. avg is a built-in function; rename the variable:
SQL> create or replace function getAvg(id1 IN number, id2 IN number)
2 return number
3 as
4 sal1 number;
5 sal2 number;
6 l_avg number;
7 BEGIN
8 select esal into sal1 from employees where eno = id1;
9 select esal into sal2 from employees where eno = id2;
10 l_avg := (sal1+sal2)/2;
11 return l_avg;
12 END;
13 /
Function created.
SQL> select * from employees;
ENO ESAL
---------- ----------
1 100
2 200
SQL> select getavg(1, 2) from dual;
GETAVG(1,2)
-----------
150
SQL>

How to dynamically construct table name

I would like to construct a query where a table name is based off of another table's column mod 12. For example:
SELECT *
FROM table_b_XX
where XX here is determined by table_a.column_a % 12.
Presuming you have such a tables:
SQL> create table table_a as
2 select 1212 as column_a from dual;
Table created.
As the following result returns 0, we need table_b_00 so I'll create it:
SQL> select mod(1212, 12) from dual;
MOD(1212,12)
------------
0
SQL> create table table_b_00 as select 'table 00' name from dual;
Table created.
SQL> create table table_b_01 as select 'table 01' name from dual;
Table created.
Now, create a function which returns ref cursor; it selects rows from a table whose name is designed by the help of the table_a contents:
SQL> create or replace function f_test return sys_refcursor
2 is
3 l_str varchar2(200);
4 rc sys_refcursor;
5 begin
6 select 'select * from table_b_' || lpad(mod(a.column_a, 12), 2, '0')
7 into l_str
8 from table_a a;
9
10 open rc for l_str;
11 return rc;
12 end f_test;
13 /
Function created.
Let's try it:
SQL> select f_test from dual;
F_TEST
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
NAME
--------
table 00
Right; that's contents of table_b_00.
Consider the following meta code:
DECLARE
n VARCHAR2(32767);
r VARCHAR2(32767);
BEGIN
SELECT column_a INTO name FROM table_a;
EXECUTE IMMEDIATE 'SELECT r FROM table_b_'||n INTO r;
END;
/

Join table in PL/SQL

Hi i would like to ask i get an error when i run this code , can i know how to fix it, i want to know the doctor for this patient when i enter the patient id, thank you. below is my code
DECLARE
patientid(3) := &pt_id;
dname doc_name%type;
BEGIN
SELECT doc_name
INTO dname
FROM doctor
JOIN patient
ON doctor.doc_id = patient.doc_id
WHERE pt_id = patientid;
DBMS_OUTPUT.PUT_LINE('He/She is the patient of Dr.' || dname);
END;
What is patient(3) supposed to be? Datatype is missing!
Though, consider something like this:
SQL> set serveroutput on
SQL> declare
2 -- No : patientid(3):=&pt_id;
3 -- Better : patientid varchar2(3) := &pt_id; -- is it VARCHAR2? or NUMBER? Who knows ...
4 -- Even better:
5 l_pt_id patient.pt_id%type := &par_pt_id;
6 l_dname doctor.doc_name%type;
7 begin
8 select d.doc_name
9 into l_dname
10 from doctor d join patient p on d.doc_id = p.doc_id
11 where p.pt_id = l_pt_id;
12
13 dbms_output.put_line ('He/She is the patient of Dr. '|| l_dname);
14 end;
15 /
Enter value for par_pt_id: 100
He/She is the patient of Dr. Luffy
PL/SQL procedure successfully completed.
SQL>

Convert string from a table to be used as a column for selection for another table

I have a table where I store records to be used as a column name for my queries where the record is an actual column on another table.
TBL_1
COL_1
==========
SAMPLE_COL
TBL_2
SAMPLE_COL_1 SAMPLE_COL2
============ ===========
ABC DEF
I'm having a problem using the record that I fetched to use as an actual column. I already tried a bunch of things like casting and using case (using case works but it's a bit of a brute force and I'm looking for a more elegant way of doing this).
This is a sample query that I have tried:
SELECT (SELECT column_1 FROM tbl_1)
FROM tbl_2
Expected output
SAMPLE_COL_1
============
ABC
Actual output
(SELECT column_1 FROM tbl_1)
============================
SAMPLE_COL_1
This is what I've tried that worked so far but a brute force technique
SELECT (
CASE
WHEN (SELECT column_1 FROM tbl_2) = 'SAMPLE_COL_1' THEN SAMPLE_COL_1
ELSE SAMPLE_COL_2
END
)
FROM tbl_2
Appreciate the help! Keep safe from COVID-19 everyone :)
It's not that easy as you'd want it to be - you'll have to use dynamic SQL. Here's an example, based on Scott's table(s).
Create a function that accepts table and column names and returns ref cursor.
SQL> create or replace function f_test
2 (par_table_name in varchar2, par_column_name in varchar2)
3 return sys_refcursor
4 is
5 rc sys_refcursor;
6 begin
7 open rc for 'select ' || dbms_assert.simple_sql_name(par_column_name) ||
8 ' from ' || dbms_assert.sql_object_name(par_table_name);
9 return rc;
10 end;
11 /
Function created.
Testing:
SQL> select f_test('dept', 'dname') from dual;
F_TEST('DEPT','DNAME
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
DNAME
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS
SQL> select f_test('dual', 'dummy') from dual;
F_TEST('DUAL','DUMMY
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
D
-
X
SQL>
Another example, with column (and table) names stored in a table (something like you posted).
Table that contains those info and the function:
SQL> select * from tbl_1;
TNAM CNAME
---- -----
dept dname
dual dummy
SQL> create or replace function f_test
2 (par_table_name in varchar2)
3 return sys_refcursor
4 is
5 l_str varchar2(1000);
6 rc sys_refcursor;
7 begin
8 select 'select ' || dbms_assert.simple_sql_name(cname) ||
9 ' from ' || dbms_assert.sql_object_name(tname)
10 into l_str
11 from tbl_1
12 where tname = dbms_assert.sql_object_name(par_table_name);
13 open rc for l_str;
14 return rc;
15 end;
16 /
Function created.
Testing:
SQL> select f_test('dept') from dual;
F_TEST('DEPT')
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
DNAME
--------------
ACCOUNTING
RESEARCH
SALES
OPERATIONS
SQL>

Converting column data into row in pl/sql

Hi When i went for an interview they asked me this question.
Create table course(Name CHAR(10));
insert into course values ('Java');
insert into course values ('Oracle');
insert into course values ('Python');
insert into course values ('C');
insert into course values ('C++');
o/p:
Java Oracle python c c++
Thanks in advance,
Sandhya.
I presume that NAME column's datatype should have been VARCHAR2, not CHAR.
Anyway, another option (similar to Tejash's LISTAGG) which uses XMLAGG and is safer if the result is larger than 4000 characters.
SQL> SELECT RTRIM (
2 XMLAGG (XMLELEMENT (e, name || ' ') ORDER BY null).EXTRACT (
3 '//text()'),
4 ',')
5 result
6 FROM course;
RESULT
------------------------------------------------------------
Java Oracle Python C C++
SQL>
Or, as you tagged the question with PL/SQL tag, then an anonymous PL/SQL block might look like this:
SQL> set serveroutput on
SQL>
SQL> declare
2 l_result varchar2(100);
3 begin
4 for cur_r in (select name from course) loop
5 l_result := l_result ||' '|| cur_r.name;
6 end loop;
7
8 dbms_output.put_line(trim(l_result));
9 end;
10 /
Java Oracle Python C C++
PL/SQL procedure successfully completed.
SQL>
Or - similarly - a function:
SQL> create or replace function f_course
2 return varchar2
3 is
4 l_result varchar2(100);
5 begin
6 for cur_r in (select name from course) loop
7 l_result := l_result ||' '|| cur_r.name;
8 end loop;
9
10 return trim(l_result);
11 end;
12 /
Function created.
SQL> select f_course from dual;
F_COURSE
--------------------------------------------------------------
Java Oracle Python C C++
SQL>
Or, a procedure with an OUT parameter:
SQL> create or replace procedure p_course (par_result out varchar2)
2 is
3 l_result varchar2(100);
4 begin
5 for cur_r in (select name from course) loop
6 l_result := l_result ||' '|| cur_r.name;
7 end loop;
8
9 par_result := trim(l_result);
10 end;
11 /
Procedure created.
SQL> declare
2 l_out varchar2(100);
3 begin
4 p_course(l_out);
5 dbms_output.put_line(l_out);
6 end;
7 /
Java Oracle Python C C++
PL/SQL procedure successfully completed.
SQL>
As you can see, quite a few options; use the one that most suits your needs.
You can use an aggregate function - LISTAGG as following:
SQL> SELECT
2 LISTAGG(TRIM(NAME), ' ') WITHIN GROUP(
3 ORDER BY
4 NULL
5 ) AS RESULT
6 FROM
7 COURSE;
RESULT
--------------------------------------------------------------------------------
C C++ Java Oracle Python
SQL>
Cheers!!