This question already has answers here:
Error in Oracle stored procedure
(2 answers)
Closed 7 years ago.
I wrote simple stored procedure in oracle. but its shows procedure created with compilation errors.
My code is:
Create PROCEDURE test
(
ID_no varchar
)
AS
BEGIN
SELECT Student.name , Student.dept, from Student
WHERE Student.id=ID_no
END;
/
Please help me to solve this problem .
1) Extra comma -
SELECT Student.name , Student.dept, from
2) semicolon after select is missing -
WHERE Student.id=ID_no
END;
3) If you get the values from query, you should put it to something -
CREATE PROCEDURE test(id_no VARCHAR) AS
s_name student.name%TYPE;
s_dept student.dept%TYPE;
BEGIN
SELECT student.name, student.dept
INTO s_name, s_dept
FROM student
WHERE student.id = id_no;
END;
/
Related
This question already has answers here:
Is it possible to return the Primary Key on an Insert as select statement - Oracle?
(2 answers)
Closed 11 months ago.
Is it possible to insert a row into a table from a SELECT statement (as opposed to VALUES syntax) and use the RETURNING clause to retrieve a column value?
Using the SELECT syntax fails with ORA-00933: SQL command not properly ended:
DECLARE
l_id users.id%TYPE;
BEGIN
INSERT INTO users (id, name)
SELECT users_seq.nextval, 'foo'
FROM DUAL
RETURNING id INTO l_id;
END;
/
It works fine with the VALUES syntax of course:
DECLARE
l_id users.id%TYPE;
BEGIN
INSERT INTO users (id, name)
VALUES (users_seq.nextval, 'foo')
RETURNING id INTO l_id;
END;
/
According to the documentation here https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/INSERT.html#GUID-903F8043-0254-4EE9-ACC1-CB8AC0AF3423 (search for single_table_insert) it is not supported in normal SQL, and I believe not in PL/SQL either.
This question already has answers here:
PL/SQL - comma separated list within IN CLAUSE
(4 answers)
Closed 4 years ago.
I need you help to pass a multivalue to a stored procedure in Oracle,
The values are created dynamically.
Basically I'm sending the values from .NET in a simple "string" and Oracle receives it as varchar2:
Stored Procedure
create or replace procedure sp_text_in
(text varchar2)
AS
CURSOR texts
IS
SELECT text FROM t_text where key_text in (text)
;
BEGIN
FOR reg IN texts LOOP
dbms_output.put_line(reg.text);
END LOOP;
end sp_text_in;
Example: the values can be:
select * from t_text where key_text in (197,198,196);
OR simply one value
select * from t_text where key_text in (197);
Ideal option is as per the link shared by #anonyXmous. Another sub optimal option is using dynamic SQL. But this could allow SQL injection. Dynamic SQL solution below.
create or replace procedure SP_TEXT_IN (TEXT varchar2)
as
type TEXTSTABTYP is table of varchar2(1000);
TEXTSTAB TEXTSTABTYP := TEXTSTABTYP();
begin
execute immediate 'select TEXT from T_TEXT where KEY_TEXT in ('||TEXT||')'
bulk collect into TEXTSTAB;
for IDX in TEXTSTAB.first..TEXTSTAB.last
loop
dbms_output.put_line(TEXTSTAB(IDX));
end loop;
end sp_text_in;
This question already has answers here:
Comma separated values to IN function in oracle
(2 answers)
PL/SQL - Use "List" Variable in Where In Clause
(3 answers)
Closed 5 years ago.
I have the following query
set serveroutput on;
declare store_nbr number(9,0);
Begin
Select trim(NODE_ID) into store_nbr from HOST_STORE_PROFILE
where sysdate between EFCV_DTE and EXPR_DTE and TO_CHAR(NODE_ID) in
('12175','22671','16449');
dbms_output.Put_line(store_nbr);
end;
This works fine and prints a value(store_nbr).
But when I tried to replace the 'in' condition in the select statement with variable declaration, it is not working as desired.
I am doing like this:
declare store_nbr number(9,0);
ca_store_nbr varchar2(4096) := '(12175,22671,25519)';
begin
Select NODE_ID into store_nbr from HOST_STORE_PROFILE
where sysdate between EFCV_DTE and EXPR_DTE and TO_CHAR(NODE_ID) in ||
ca_store_nbr;
dbms_output.Put_line(store_nbr);
end;
I don't get the value into store_nbr.
Whats wrong with the second query.
This needs to be a dynamic sql statement.
make a string out of the whole statement and execute with something like
EXECUTE IMMEDIATE <sql statement>
This question already has an answer here:
delete statement not deleting records
(1 answer)
Closed 5 years ago.
Anyone know why my exceptions aren't working? When i test the sql the only exception that appears is the when others.
This is my first stored procedure, i want it to insert new students using parameter values.
create or replace PROCEDURE ADD_STUDENT_TO_DB (pSTUid number, pSTUname varchar2) AS
Out_Of_Range Exception;
BEGIN
IF pSTUid <=1 AND pSTUid >=499 THEN
RAISE Out_Of_Range;
END IF;
INSERT INTO STUDENT (STUid, STUname)
VALUES (pSTUid, pSTUname);
EXCEPTION
WHEN DUP_VAL_ON_iNDEX THEN
dbms_output.put_line('-20010. Duplicate student ID');
WHEN Out_Of_Range THEN
dbms_output.put_line('-20002. student ID out of range');
WHEN OTHERS THEN
dbms_output.put_line('-20000. Error');
END;
This is my second stored procedure, i want this to call my first sp.
create or replace PROCEDURE ADD_STUDENT_VIASQLDEV (pSTUid number, pSTUname varchar2) AS
BEGIN
INSERT INTO STUDENT (STUid, STUname)
VALUES (pSTUid, pSTUname);
dbms_output.put_line('--------------------------------------------');
dbms_output.put_line('Adding student ID: ' || pSTUid || ' Name: ' || pSTUname);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('-20000. Error');
END;
there should exist IF pSTUid <=1 OR pSTUid >=499 THEN instead of your statement for out_of_range
This question already has answers here:
Calling a stored PROCEDURE in Toad
(2 answers)
Closed 7 years ago.
I have delcared function like this:
CREATE or replace PROCEDURE proc
(
P_ID IN INTEGER,
NAME OUT CHAR,
SURNAME OUT CHAR,
TOTAL OUT CHAR
)
AS
BEGIN
SELECT NAME, SURNAME, sum(TOTAL) AS TOT
INTO NAME,SURNAME,TOTAL
FROM STATISTICS, PLAYERS, PERSON
WHERE STATISTICS.SID=P_ID AND PERSON.ID=PLAYERS.SID AND
STATISTICS.PLAYERS_SID=PLAYERS.SID
GROUP BY NAME,SURNAME;
END;
Select statement works corectly, but how to call this procedure in Oracle?
I tried something like
EXEC proc(4);
AND
DECLARE
NAME OUT CHAR,
SURNAME OUT CHAR,
TOTAL OUT CHAR
BEGIN
P_ID := 12 ;
proc (
P_ID => P_ID,
NAME => NAME,
SURNAME => SURNAME,
TOTAL => TOTAL
);
END;
but without any success.
EXEC proc(4);
EXECUTE is a SQL*Plus command.
You have following options:
EXECUTE in SQL*Plus
Call it in an anonymous PL/SQL block.
Run in SQL Developer client tool
Let's see all the three ways:
In SQL*Plus:
SQL> variable v_ename varchar2(20);
SQL> exec get_emp(7788, :v_ename);
PL/SQL procedure successfully completed.
SQL> print v_ename;
V_ENAME
--------------------------------
SCOTT
In an anonymous PL/SQL block:
SQL> CREATE OR REPLACE PROCEDURE get_emp(
2 i_empno IN emp.empno%TYPE,
3 o_ename OUT emp.ename%TYPE)
4 AS
5 BEGIN
6 SELECT ename INTO o_ename FROM emp WHERE empno = i_empno;
7 END;
8 /
Procedure created.
SQL> SET serveroutput ON
SQL> DECLARE
2 v_ename VARCHAR2(20);
3 BEGIN
4 get_emp(7788, v_ename);
5 dbms_output.put_line('Employee name is '||v_ename);
6 END;
7 /
Employee name is SCOTT
PL/SQL procedure successfully completed.
In SQL Developer client tool:
Go to connections on the left pane.
Expand the Procedures.
Right click on the procedure and select "Run".
It will open a new window, provide the Input value and click OK.
The output will be shown in Output Log at the bottom as "Output Variables".