PL/SQL tutorial 42: How To Create PL/SQL Stored Procedure With Parameters In Oracle Database

PL/SQL tutorial 42: How To Create PL/SQL Stored Procedure With Parameters In Oracle Database

Manish Sharma

7 лет назад

88,308 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@gurukiran5605
@gurukiran5605 - 17.01.2024 06:33

Select Sal from emp where row num = 3

Ответить
@tanmaykumbhar9614
@tanmaykumbhar9614 - 24.09.2023 15:09

Can we select statement in procedure?? To get out put of cretain coloumns..??

Ответить
@blaisemugisha8990
@blaisemugisha8990 - 16.11.2022 12:19

your tone makes one learn easily

Ответить
@eshwerm569
@eshwerm569 - 15.09.2022 12:52

guys plz any one help me to get free certification from oracle as developer

Ответить
@neerajjoshi7639
@neerajjoshi7639 - 14.07.2022 12:58

Thanks manish for such a invaluable lectures.
does anybody know how to copy one table's field into another table using stored procedure.
well i have tried but occurs an errror.

create or replace procedure table_copy(
source_table varchar2,
target_table varchar2)
is
begin
execute immediate 'insert into '||target_table||' (select * from '||source_table||')';
end;

execute table_copy (source_table,target_table);

error popping is : PLS-00357: Table,View Or Sequence reference 'table_name' not allowed in this context

Ответить
@manishbagal2178
@manishbagal2178 - 12.02.2022 14:53

bhai manish u cant describe how to exec this emp_sal procedure

Ответить
@kalyanisarode4878
@kalyanisarode4878 - 12.12.2021 17:20

How to print two tables in stored procedures using a dbms_output.put_line statement ... Please provide me this answer as possible as

Ответить
@faizanansari582
@faizanansari582 - 21.09.2021 09:04

How to create same with the select query?

Ответить
@coolguycoolguy4849
@coolguycoolguy4849 - 17.09.2021 06:09

--3rd highest salary of the employees
CREATE OR REPLACE PROCEDURE third_hg_salary IS
CURSOR cr_empSal IS
SELECT ename, sal FROM emp
ORDER BY sal desc;

var_empSal cr_empSal%ROWTYPE;
var_counter NUMBER := 1;
BEGIN
OPEN cr_empSal;
FOR var_counter IN 1..3 LOOP
FETCH cr_empSal INTO var_empSal;
EXIT WHEN cr_empSal%NOTFOUND;
IF var_counter = 3 then
DBMS_OUTPUT.PUT_LINE(var_empSal.eName ||' '|| var_empSal.sal);
END IF;
END LOOP;
CLOSE cr_empSal;
END;
/

EXEC third_hg_salary;

Ответить
@SivaKumar-rv1nn
@SivaKumar-rv1nn - 25.07.2021 10:54

Thankyou sir

Ответить
@wasifansari8225
@wasifansari8225 - 29.06.2021 22:43

CREATE OR REPLACE PROCEDURE NTHSALARY ( N INTEGER)
IS
SALARY SCOTT.EMP.SAL%TYPE;
BEGIN
SELECT SAL INTO SALARY FROM (SELECT DISTINCT SAL FROM (SELECT SAL,DENSE_RANK() OVER(ORDER BY SAL DESC) AS DRANK FROM SCOTT.EMP) WHERE DRANK=N);
DBMS_OUTPUT.PUT_LINE('SALARY OF RANK' || N || '=' || SALARY);
END NTHSALARY ;
/
EXEC NTHSALARY(3);

Ответить
@vishraj2460
@vishraj2460 - 24.05.2021 14:55

I have to pass. Table parameter to oracle store procedure

Ответить
@sruthiraghu8568
@sruthiraghu8568 - 23.05.2021 12:26

How to execute?

Ответить
@aravinthvs2726
@aravinthvs2726 - 27.01.2021 06:39

create or replace procedure usp_max_sal (n number) is
v_salary number:=0;
BEGIN
SELECT Salary into v_sal
FROM employees W1
WHERE n = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM employees W2
WHERE W2.Salary >= W1.Salary
);
DBMS_OUTPUT.PUT_LINE(v_sal||' is the '||n||' highest salary');
END;

Then execute it

SET SERVEROUTPUT ON;
EXEC usp_max_sal(n);

Thanks

Ответить
@subhashisoracle3944
@subhashisoracle3944 - 06.01.2021 19:38

SQL> create or replace procedure my_poc (p_rank number)
2 as
3 cursor mycursor is
4 select * from (select ename,sal,dense_rank() over(order by sal desc)rank
5 from emp)where rank<=p_rank;
6 begin
7 for myindex in mycursor loop
8 dbms_output.put_line(myindex.ename||' sal is '||myindex.sal);
9 end loop;
10 end;
11 /

Procedure created.

SQL> exec my_poc(3);
KING sal is 5000
SCOTT sal is 3000
FORD sal is 3000
JONES sal is 2975

Ответить
@Srikrishnasundar
@Srikrishnasundar - 07.12.2020 06:59

i wanted to create a PL SQL code for sending zip file in a mail as attachment. but I am not able to do this, Can you help me here Manish???? @Manish Sharma

Ответить
@jadevid598
@jadevid598 - 10.11.2020 19:16

TOP

Ответить
@ramanangi5660
@ramanangi5660 - 18.10.2020 14:09

create or replace procedure third_sal(dense_ran number)
as
v_sal emp.sal%type;
begin
select sal into v_sal from(select sal,dense_rank()over(order by sal desc)r from emp)where r=dense_ran;
dbms_output.put_line('the'||dense_ran||'th highest sal is'||v_sal);
end;
/
begin
third_sal(&dens_rank);
end;

Ответить
@ramasettidivyakranth1998
@ramasettidivyakranth1998 - 05.08.2020 13:34

Thanks manish for your knowledge sharing.
Short and accurate code for nth highest salary.just pass the n value what ever you want ;-)
create or replace procedure n_highsal(n number) is
vsal number:=0;
begin
select salary into vsal from
(select salary,dense_rank()
over(order by salary desc) r from employees)where r = n;
dbms_output.put_line(vsal||' is the '||n||' highest salary');
end;

Ответить
@ibrahimmustafa4692
@ibrahimmustafa4692 - 19.09.2019 22:13

First of All a big thank to Manish.
I solve the question in a dynamic way by using a PROCEDURE with n parameter and it includes CURSOR base record variable and FOR LOOP. n represents the order of the salary noting that salaries are ordered descendingly.
CREATE OR REPLACE PROCEDURE n_high_salary
(n NUMBER) IS
CURSOR cur_salary IS
SELECT salary FROM employee ORDER BY salary DESC;
v_salary cur_salary%ROWTYPE;
counter NUMBER(10) := 1;
BEGIN
FOR v_salary IN cur_salary LOOP
IF counter = n THEN
DBMS_OUTPUT.PUT_LINE('The highest '||n||' salary is '||v_salary.salary);
EXIT;
END IF;
counter := counter + 1;
END LOOP;
END n_high_salary;
/


to see the 3rd highest salary just execute the procedure in a block as follows:
BEGIN
n_high_salary(3);
END;
/

Ответить
@boyantodorov3454
@boyantodorov3454 - 19.09.2019 11:07

CREATE OR REPLACE PROCEDURE nm_highest_sal (nm_of_salary NUMBER)
IS
v_salary number:=0;
BEGIN
SELECT salary
INTO v_salary
FROM
(SELECT salary, ROWNUM as RN
FROM (SELECT DISTINCT employees.salary
FROM employees
ORDER BY 1 DESC))
WHERE RN = nm_of_salary;
DBMS_OUTPUT.PUT_LINE(v_salary ||' is the ' || nm_of_salary ||' highest salary.');
END;
/

Ответить
@pavi_thra_gowda
@pavi_thra_gowda - 29.07.2019 16:57

create or replace procedure proc_1
is
e_name varchar2(20);
salary number;
begin
select max(sal) into salary from emp22 where sal < (select max(sal) from emp22 where sal < (select max(sal) from emp22));
select ename into e_name from emp22 where sal = salary;
dbms_output.put_line( 'Third highest salary is ' || salary ||' drawn by '|| e_name );
end;
/

set serveroutput on;
execute proc_1;

Ответить
@nalinaksheepanda1596
@nalinaksheepanda1596 - 02.06.2019 13:35

create or replace procedure nth_highest_sal(which_sal number)
is
var_nth_sal employees.salary%type;
begin
select distinct salary into var_nth_sal from employees a where which_sal = (select count(distinct salary) from employees b
where b.salary >= a.salary) order by a.salary desc;
dbms_output.put_line(which_sal||'th salary of employee is '||var_nth_sal);
end;
/
begin
nth_highest_sal(3);
end;
/

Ответить
@prabhatshrama5499
@prabhatshrama5499 - 15.05.2019 20:43

Here is one solution,


create or replace procedure hi_sal(sal number:=3)
is
sal12 number;
begin
select distinct salary into sal12 from employees e1 where 3 = (select count(distinct salary) from employees e2 where e1.salary<=e2.salary);
dbms_output.put_line(sal12);
end;
/

Ответить
@debashisbehera7169
@debashisbehera7169 - 08.03.2019 07:24

what is the differnce between IS and AS ? Where it is used ?

Ответить
@satyabratakar2915
@satyabratakar2915 - 07.01.2019 23:42

Create or REPLACE PROCEDURE third_hig_Salary(sal out Number) IS
Begin
Select max(salary) INTO sal from employee
WHERE salary<(Select MAX(salary) FROM employee
where salary<(select max(salary) from employee));
DBMS_OUTPUT.put_line('Third Highest Salary is '||sal);
ENd third_hig_Salary;
/
VARIABLE sala Number;
EXECUTE third_hig_salary(:sala);

Ответить
@karishmapawar7641
@karishmapawar7641 - 19.11.2018 13:59

what happens next? can you show how to execute it further

Ответить
@ashefshahrior5437
@ashefshahrior5437 - 07.09.2018 18:06

I want to create a stored procedure with the capability of handling variable number of arguments. Suppose that the procedure sometimes may be- "PROCC(a,b,c)" and some other time it may be "PROCC(a,b)". How to do that? It'd be great if anyone could provide me with a demo code capable of doing so. Thanks in advance.

Ответить
@stjepanmudronja6997
@stjepanmudronja6997 - 02.09.2018 14:32

create or replace procedure pr_third_sal (var_third number)
is
var_salary employees.salary%type;
begin
SELECT salary
INTO var_salary
FROM employees
ORDER BY salary desc
OFFSET 2 ROWS FETCH NEXT 1 ROWS ONLY;
dbms_output.put_line('Third Highest salaray of the employees is:'||var_salary);
END;
/

EXEC pr_third_sal(1);

Ответить
@yashkumar0042
@yashkumar0042 - 01.09.2018 18:22

you are the best tutor Manish Sharma. God bless you bro.
The question which i was thinking, you were replying.
Note: This is not only related to this videos but also others plsql videos as well.

Ответить
@BullishBuddy
@BullishBuddy - 09.07.2018 18:02

Best PL/SQL video I've ever seen

Ответить
@BullishBuddy
@BullishBuddy - 09.07.2018 18:02

great job!

Ответить
@anupamkumartejaswi9210
@anupamkumartejaswi9210 - 15.06.2018 10:01

create or replace procedure emp_third_highest_Sal(var_dept_id number)
is
v_employee_id EMPLOYEES.EMPLOYEE_ID%TYPE;
v_employee_name EMPLOYEES.FIRST_NAME%TYPE;
v_employee_salary EMPLOYEES.SALARY%TYPE;
v_employee_dept EMPLOYEES.DEPARTMENT_ID%TYPE;
begin
select employee_id,first_name,salary,department_id into v_employee_id,v_employee_name,v_employee_salary,v_employee_dept
from (select employee_id,first_name,salary,department_id,dense_rank() over(PARTITION by department_id order by salary desc) as rank
from EMPLOYEES) where rank=3 and department_id=var_dept_id;
dbms_output.put_line(v_employee_id||' '||v_employee_name||' '||v_employee_salary||' '||v_employee_dept);

--fully working and tested code for dept wise third highest salary using window function.



end;

Ответить
@nilutpaldutta8791
@nilutpaldutta8791 - 26.02.2018 22:01

CREATE OR RPLACE PROCEDURE high_sal (first_high NUMBER , second_high NUMBER)
IS
BEGIN
SELECT MAX(salary) from employees where salary != first_high AND salary != second_high;
END high_sal;
/

--------------------------------------------

DECLARE
a NUMBER;
b NUMBER;
BEGIN
SELECT MAX(salary) INTO a FROM employees;
SELECT MAX(salary) INTO b FROM employees WHERE salary!= a;
high_sal(a,b);
END;
/

Ответить
@vivekkhurpe1005
@vivekkhurpe1005 - 19.02.2018 20:13

---

Create Or Replace PROCEDURE High3sal
Is
VSAL EMP.SAL%TYPE;
Begin
Select Sal Into Vsal From Emp Order By Sal Desc
Offset 3 Rows
Fetch NEXT 1 Row Only;
DBMS_OUTPUT.PUT_LINE(VSAL);
End;
EXECUTE HIGH3SAL

---

Ответить
@davidbanner6440
@davidbanner6440 - 18.01.2018 14:04

In case you're wondering the outcome of this SP e.g:

SET SERVEROUTPUT ON;

EXECUTE emp_sal (90, 10);

SELECT * FROM EMPLOYEES;

you should see in employees table, that all employees with department_id = 90 had their salary increased 10X.

Ответить
@ateeqrehman7873
@ateeqrehman7873 - 14.01.2018 20:50

Sir please solve this error
create procedure ffirst is
var_name varchar(20) :='Ateeq';
var_reg varchar(20) :='15-SE-104';
begin
dbms_output.put_line('Hellow world my name is '||var_name ||'Registration Number is' ||var_reg);
end ffirst;
execute ffirst;

Error:
create procedure ffirst is
var_name varchar(20) :='Ateeq';
var_reg varchar(20) :='15-SE-104';
begin
dbms_output.put_line('Hellow world my name is '||var_name ||'Registration Number is' ||var_reg);
end ffirst;
execute ffirst;

Ответить
@mykhailocholii2903
@mykhailocholii2903 - 07.12.2017 14:00

VSAP INTERNET!!!!

Ответить
@prabhur764
@prabhur764 - 26.10.2017 17:09

can you give your mobile number to contact you?

Ответить
@bharathipanamala9919
@bharathipanamala9919 - 08.07.2017 22:13

create or replace procedure proc_third_high_Sal(var_rank Number)
is
var_emp_id employees.employee_id%type;
var_salary NUMBER;
Begin
select employee_id, salary into var_emp_id, var_salary from (
select employee_id,salary, dense_rank() over (order by salary desc) as sal_rank from employees
)
where sal_rank = var_rank;
dbms_output.put_line('Third Highest salaray of the employees is:'||var_salary);
End;
/

Ответить
@sathiyaseelan1164
@sathiyaseelan1164 - 10.05.2017 09:23

Here is another way,

CREATE OR REPLACE PROCEDURE PP IS
CURSOR THIRD IS SELECT * FROM EMPLOYEES
WHERE SALARY=(SELECT MAX(SALARY) FROM EMPLOYEES
WHERE SALARY<(SELECT MAX(SALARY) FROM EMPLOYEES
WHERE SALARY<(SELECT MAX(SALARY) FROM EMPLOYEES)));
BEGIN
FOR I IN THIRD
LOOP
DBMS_OUTPUT.PUT_LINE('THE 3RD SALARY IS:'||I.SALARY);
END LOOP;
END;

Ответить
@subhajena5982
@subhajena5982 - 29.01.2017 00:45

3RD HIGEST SAL


CREATE OR REPLACE PROCEDURE THIRD_HIGH_SAL
AS
CURSOR THIRD IS
SELECT * FROM EMP A WHERE 3=
(SELECT COUNT(DISTINCT SAL) FROM EMP B WHERE A.SAL <= B.SAL);
BEGIN
FOR I IN THIRD LOOP
DBMS_OUTPUT.PUT_LINE('EMP 3RD HIGEST SAL IS::'||I.SAL);
END LOOP;
END THIRD_HIGH_SAL;

Ответить
@rajraj-rv6ii
@rajraj-rv6ii - 08.01.2017 14:02

Hi Manish,

Below is the assignment

create or replace procedure nth_highest (nth number) is
v_salary number;
v_sql varchar(1000);
begin
v_sql:= 'select a.salary from employees a where '||nth||' = (select count(distinct(b.salary)) from employees b where a.salary<=b.salary)';
execute immediate v_sql into v_salary;
dbms_output.put_line('the'||' '||nth||' '||'highest salary is'||':'||v_salary);
end;

I will pass either 1, 2 , 3 .... and get the output correspondingly for 1st , 2nd, 3rd...... highest salaries respectively.

Ответить
@vinod901
@vinod901 - 13.10.2016 19:28

Hi Manish,

Please check this for third highest salary :

create or replace procedure proc_third_high_sal
is

var_third_high EMPloyees.SALARY%type;

begin

select salary into var_third_high from (select salary, rownum as rownumber from
(select distinct salary from EMPloyees order by salary desc)) where
rownumber=3;

dbms_output.put_line('third highest sal is ' || var_third_high);

end;

thanks a lot man for tutorials :)

Ответить
@samnang89
@samnang89 - 20.09.2016 04:40

Manish , could you make tutorial about RMAN ?

Ответить
@manishchauhan880
@manishchauhan880 - 14.09.2016 10:10

Manish help me ..is there high scope for us. I mean that SQL or plsql skill holder ?

Ответить