Stored procedure in oracle sql - sql

I'm trying to do a very simple stored procedure for a class project. I just want to use it to execute a simple insert statement.
What I have is:
CREATE OR REPLACE PROCEDURE insertSpa (p_spaID IN INTEGER, s_sdate IN VARCHAR2, p_address IN VARCHAR2)
AS
BEGIN
INSERT INTO spa( spaID,sdate,address)
VALUES (p_spaid, p_sdate, p_address);
END;
/
And then to call it, I've been using:
exec insertSpa( p_spaID => 9999, p_sDate => '12122012', p_address => '1234 Main St. 12345' );
When I try and run the first portion, I get an error that says "Procedure created with compilation errors." Here is what the spa relation is:
SPAID : NOT NULL NUMBER(38)
SDATE : VARCHAR2(8)
ADDRESS : VARCHAR2(100)

It appears that, among other things, you are trying to use SQL Server's T-SQL syntax in Oracle rather than PL/SQL. I'm guessing that spaid is intended to be an integer, sdate is intended to be an integer (though since it appears to represent a date, it really ought to be defined as a date), and address is intended to be a string. I'm also guessing that the spa table has three columns spaID, sDate, and address. If those guesses are accurate, something like
CREATE OR REPLACE PROCEDURE insertSpa( p_spaID IN INTEGER,
p_sdate IN INTEGER,
p_address IN VARCHAR2 )
AS
BEGIN
INSERT INTO spa( spaID, sDate, address )
VALUES( p_spaID, p_sdate, p_address );
END;
should compile. You would then call it
SQL> exec insertSpa( p_spaID => 9999, -
p_sDate => 12122012, -
p_address => '1234 Main St. 12345' );
The error that you are getting seems to imply that you never created the stored procedure. Did you actually run the CREATE PROCEDURE statement? It sounds like maybe you just created the script on your local client machine but then never executed it to create the procedure. You can run the CREATE PROCEDURE statement directly in SQL*Plus. Or you can save it to a file and execute the script in SQL*Plus
SQL> #<<name of .sql script>>

Presumably, from what you've shown, the procedure compiles OK, and it's the exec - which is just shorthand for an anonymous block - that says there is an error. It would have been helpful if you'd shown the error stack. (If the procedure, or any named blocked, said it had a compilation error, you could get the details with show errors, or by selecting from the user_errors view).
You created your procedure with the second parameter named s_sdate:
CREATE OR REPLACE PROCEDURE insertSpa (p_spaID IN INTEGER,
s_sdate IN VARCHAR2, p_address IN VARCHAR2)
... but when you call it you're saying the name is p_sDate:
exec insertSpa( p_spaID => 9999, p_sDate => '12122012', ...
The names have to match. Given the other names, presumably the procedure declaration should change to p_sDate. Actually, you have a mismatch between the procedure declaration and where you're using it in the procedure, so you'd get both errors - on compile and on exec - so you should definitely change it in the declaration. I missed that you'd said you had a procedure compilation, somehow.

Related

Oracle Apex: Success With compilation error

I'm trying to create a procedure named "Greetings" in the oracle apex. The Procedure Greetings runs with some error throwing up called "Success with compilation error". Is there anything wrong with my below code.
Code:
create table tb_Animals (
txt_Name varchar(20) Primary Key,
int_Weight number
);
insert into tb_Animals values ('Dog',30);
insert into tb_Animals values ('Cat',15);
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
select * from tb_Animals;
END;
In PL/SQL, you have to insert those values into something, usually a variable, However, if you're selecting the whole table contents, then it has to be something else; one option might be a refcursor. For example:
SQL> CREATE OR REPLACE PROCEDURE greetings
2 AS
3 rc sys_refcursor;
4 BEGIN
5 open rc for
6 select * from tb_Animals;
7 END;
8 /
Procedure created.
SQL>
It is now correct as far as compilation is concerned, but - actual "solution" depends on what you really want to do.
Two problems I see here:
The code you provided is a SQL script that will create the procedure, not run the procedure. The error is telling you that the procedure has been created, but that it has an error. You can see the precise error by entering "SHOW ERRORS" at the sqlplus prompt after the create command completes.
The problem with the procedure itself is that you have to do something with the data you've selected. It needs to be processed into variables, or used in a for/next loop for some purpose. Simply selecting data into nothing won't work - PL/SQL is a programming language, not a scripting language. See here for a beginner's guide: https://oracle-base.com/articles/misc/introduction-to-plsql. I can't offer any more specific guidance than that since you've not provided any info on what your procedure is actually trying to do.
If you just want your script to return the data you just inserted into the table as a confirmation, just run the select statement without the procedural stuff, like this (and don't forget to commit your changes!):
create table tb_Animals (
txt_Name varchar(20) Primary Key,
int_Weight number
);
insert into tb_Animals values ('Dog',30);
insert into tb_Animals values ('Cat',15);
commit;
select * from tb_Animals;

Is this possible to promt everytime with stored procedure execution in PL/SQL?

Here is simple procedure for insert data into table is:-
create or replace
PROCEDURE add_job_history
( p_emp_id job_history.employee_id%type
, p_start_date job_history.start_date%type
, p_end_date job_history.end_date%type
, p_job_id job_history.job_id%type
, p_department_id job_history.department_id%type
)
IS
BEGIN
INSERT INTO job_history (employee_id, start_date, end_date,job_id, department_id)
VALUES(p_emp_id, p_start_date, p_end_date, p_job_id, p_department_id);
END add_job_history;
Now i am Execute this stored procedure with parameter it will works...
but i want to make this procedure like everytime i am execute it & it shows me prompt and ask the value of colums.
Is this possible?
Like an example:-
exec add_job_history;
enter your employee_id:15
enter your start_date:
Help me out if possible.i am learning PL/SQL & confused with it.
No. Stored procedures run on the server and have no way of prompting the user for input. They have no idea what client application, if any, is being used to call the stored procedure.
You could write a SQL*Plus script that prompted the user for input and then called the stored procedure by passing in those values. That script would have to exist on every client machine.

insert statement inside stored procedure failing

I have written a oracle stored procedure and creating an error log table if that doesn't exist.
SELECT COUNT(*)
INTO v_count
FROM all_tables
WHERE TABLE_NAME = 'ERROR_LOG';
IF v_count =0 THEN
cr_table := 'CREATE TABLE ERROR_LOG ( ERROR_LOG_ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY, IDENTIFIER VARCHAR2(100), ERROR_MESSAGE VARCHAR2(1000),created_by varchar2(100 ), created_date TIMESTAMP DEFAULT systimestamp )';
execute immediate cr_table;
then issuing an insert statement below in the code as
INSERT
INTO error_log
(
identifier,
error_message,
created_by
)
VALUES
(
v_identifier,
'Success',
v_user
);
But the SP is throwing compilation error with
PL/SQL: ORA-00942: table or view does not exist
If I create the table manually, offline, and compile then it works.
any help?
if you use execute immediate to create a table, it exists in the PL/SQL context, not in SQL context, you have to execute the INSERT by using execute immedation too.
Or you have to make the insert inside an other BEGIN.. END block.
Before using the stored procedure, please create a view or table where you are going to insert the values using the SP. So it will avoid the compiler error in the run time.
This is too long for a comment.
The table doesn't exist when the code is compiled. Hence, you are getting a compile-time error. At compilation time, Oracle doesn't know that the table will exist when executed.
One solution is to use dynamic SQL for the insert as well.
A better solution is to set up the database with the appropriate tables before the code can ever be executed. Creating permanent tables dynamically in conditional code is usually a sign of a poorly designed application.

Trying to insert data into various tables via stored procedure, what am i doing wrong?

I am trying to get a JAVA program (Pega to be exact) to call the procedure to load XML data by calling a stored procedure, but it is not working, what am i doing wrong? I know it has something to do with my variable definitions but i am not sure how i would be able to specify that they will be provided parameters by the java file. below is my stored procedure. Thanks in advance
The error messages I'm getting are:
Error(2,16): PLS-00103: Encountered the symbol ";" when expecting one of the following: := . ) , # % default character
and
Error(16,1): PLS-00103: Encountered the symbol ")" when expecting one of the following: begin function pragma procedure subtype type current cursor delete exists prior
Create or Replace Procedure Cascade_Load (
Value_ID Number,
pValue_ID Number,
pCalculation_ID Number,
Calculation_ID Number,
Calculation_Value_ID Number,
p_Entity_Address_ID Varchar2(50),
New_Value_ID Number,
New_Calculation_ID Number,
New_Calculation_Value_ID Number
) AS
BEGIN
IF code is not null
THEN
INSERT INTO Value (Value_ID, energy_product_id, data_source_id, unit_cd,
value_tx, padd_cd, supply_type_id, country_cd, state_cd, county_cd,
entity_address_id, series_id, energy_process_cd, result_type_cd,
geo_area_cd, create_dt, create_user_id)
VALUES (
Value_ID,
Get_energy_product_id(:NEW.Product_Name_Cd),
Get_Data_Source_Id(:NEW.Data_Source_Tx),
:NEW.UNIT_CD ,
:NEW.Value_Tx,
:NEW.PADD_CD,
Get_Supply_Type_Id(:NEW.Supply_Type_Tx),
:NEW.COUNTRY_CD,
Get_State_CD(Get_entity_Id(p_Entity_Address_ID)),
'NA',
Get_Entity_Address_ID(Get_Entity_ID(p_Entity_Address_ID)),
0,
:NEW.Energy_Process_CD,
:NEW.Result_Type_CD,
:NEW.Geo_Area_Cd,
Sysdate,
'15'
);
Commit;
END IF;
END;
#icerabbit
Am not sure if it is a syntax error for having semicolons to separate parameters instead of comas. If this was the case You would've got an error message while compiling itself.
What is that code in the body of the SP, is that a variable which is not defined?
If Value is a table name, try enclosing it in the braces.
If you are seeing an error message, please post that too, it'll give some idea for debugging further.
Thank you
:NEW and :OLD variables are used just in TRIGGERS.
You can't BIND then inside a stored procedure.
Replace the semicolon with comma in your stored procedure's parameters

How to Execute stored procedure from SQL Plus?

I have a stored procedure in oracle and want to test it from SQLPlus.
If I use
execute my_stored_proc (-1,2,0.01)
I get this error
PLS-00306: wrong number or types of arguments in call to my_stored_proc
The beginning for the proc is this
create or replace PROCEDURE my_stored_proc
( a IN NUMBER,
b IN NUMBER,
c IN NUMBER,
z out NUMBER
) AS ....
Do I need to provide the a var for the out parameter, is so how? I tried:
var z NUMBER;
But get this error when I try to run the proc
execute my_stored_proc (-1,2,0.01,z)
PLS-00201: identifier 'Z' must be declared
Also when I was in SQL-Developer it gave me the usage and it show the inputs in reverse order, that is:
execute my_stored_proc(z number,c number,b number,a number);
Do you provide them in reverse order or is that just something with SQL-Developer
I did not write the procedure and I don't normally deal with them so I could be missing something obvious.
Thanks
You have two options, a PL/SQL block or SQL*Plus bind variables:
var z number
execute my_stored_proc (-1,2,0.01,:z)
print z
You forgot to put z as an bind variable.
The following EXECUTE command runs a PL/SQL statement that references a stored procedure:
SQL> EXECUTE -
> :Z := EMP_SALE.HIRE('JACK','MANAGER','JONES',2990,'SALES')
Note that the value returned by the stored procedure is being return into :Z