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>
Related
I have table 'Studies' (with columns: student_id, name, surname, course_name, mark) and I have a task to write a PL/SQL program, where will be WITH word + FUNCTION word + PROCEDURE word.I decided to make such a program: the function will calculate the average mark for some course (input parameter) and the procedure will display information about students whose mark in this course is higher than the average. I managed to create a function that returns the average score,
create or replace FUNCTION average_mark(co_name IN VARCHAR2) RETURN REAL IS
iter NUMBER := 0;
aver NUMBER := 0;
CURSOR c1
IS
SELECT mark
FROM studies
WHERE course_name = co_name;
BEGIN
FOR student IN c1
LOOP
iter := iter + 1;
aver := aver + student.mark;
END LOOP;
RETURN ROUND((aver/iter),2);
END above_average_mark;
and procedure which displays information about a student whose mark in the course is more than a certain one, how now to connect the procedure and the function and the WITH word?
CREATE OR REPLACE PROCEDURE above(co_name IN VARCHAR2) IS
CURSOR c2
IS
SELECT *
FROM studies
WHERE course_name = co_name;
BEGIN
FOR student IN c2
LOOP
IF (student.mark > 4) THEN
DBMS_OUTPUT.PUT_LINE('name: ' || student.student_name || ', mark: ' || student.mark);
END IF;
END LOOP;
END;
i need something like this:
WITH
PROCEDURE
FUNCTION
I don't have your tables to illustrate it, so I'll use Scott's sample schema to calculate average salaries for departments.
As you said that you need something like WITH PROCEDURE FUNCTION, the only thing you have to do is to follow syntax.
Therefore, here you are: with factoring clause in this example contains a procedure which displays department name and average salary; function calls the procedure (and passes department number and average salary it calculated). Also, as any other function it actually returns a value.
SQL> set serveroutput on;
SQL> with
2 procedure p_deptno (par_deptno in dept.deptno%type,
3 par_avgsal in number)
4 is
5 l_dname dept.dname%type;
6 begin
7 select dname into l_dname
8 from dept
9 where deptno = par_deptno;
10 dbms_output.put_line('Average salary for department ' || l_dname ||
11 ' = ' || par_avgsal);
12 end p_deptno;
13
14 function f_avgsal (par_deptno in dept.deptno%type)
15 return number
16 is
17 l_avgsal number;
18 begin
19 select round(avg(e.sal)) into l_avgsal
20 from emp e
21 where e.deptno = par_deptno;
22
23 p_deptno (par_deptno, l_avgsal);
24
25 return l_avgsal;
26 end f_avgsal;
27 select f_avgsal (a.deptno) avg_sal
28 from dept a;
29 /
Result:
AVG_SAL
----------
2917
2175
1567
Average salary for department ACCOUNTING = 2917
Average salary for department RESEARCH = 2175
Average salary for department SALES = 1567
Average salary for department OPERATIONS =
SQL>
Now, adjust it to your tables & data.
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>
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!!
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!!
If I wanted to replace the * with a column name, what would it be?
create type mytable$t as table of number;
/
declare
mytmou mytable$t := myTable$T();
cnt pls_integer ;
begin
mytmou := myTable$T(1,2,3,4,5,6);
SELECT count(*) into cnt From Table (mytmou);
dbms_output.put_line(cnt);
end;
6
COLUMN_VALUE is the name of the column
SQL> ed
Wrote file afiedt.buf
1 declare
2 mytmou mytable$t := myTable$T();
3 cnt pls_integer ;
4 begin
5 mytmou := myTable$T(1,2,3,4,5,6);
6 SELECT count(column_value) into cnt From Table (mytmou);
7 dbms_output.put_line(cnt);
8* end;
SQL> /
6
PL/SQL procedure successfully completed.