I have a stored procedure which is being called from a dbms job. i.e.
DBMS_SCHEDULER.RUN_JOB ('Procedure_JB', FALSE);
A java code stored procedure, which after doing some stuff, kicks off Procedure_JB asynchronously. And then this Procedure_JB calls Procedure_PRogram and then the program would call the stored procedure.
How can i pass arguments to my stored procedure?
The arguments which i have to pass to the job are from java.
Define your job Procedure_JB to accept arguments. Then use dbms_scheduler.set_job_argument_value to define the value of the arguments you want to pass into the program your job is going to run. An example (taken from https://forums.oracle.com/forums/thread.jspa?threadID=483135)
-- create a stored procedure with two arguments
create or replace procedure myproc (arg1 in varchar2, arg2 in varchar2)
is BEGIN null; END;
/
-- create a program with two arguments and define both
begin
dbms_scheduler.create_program
(
program_name=>'myprog',
program_action=>'myproc',
program_type=>'STORED_PROCEDURE',
number_of_arguments=>2, enabled=>FALSE
) ;
dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>1,
argument_type=>'VARCHAR2',
DEFAULT_VALUE=>'13');
dbms_scheduler.DEFINE_PROGRAM_ARGUMENT(
program_name=>'myprog',
argument_position=>2,
argument_type=>'VARCHAR2');
dbms_scheduler.enable('myprog');
end;
/
-- create a job pointing to a program and set both argument values
begin
dbms_scheduler.create_job('myjob',program_name=>'myprog');
dbms_scheduler.set_job_argument_value('myjob',1,'first arg');
dbms_scheduler.set_job_argument_value('myjob',2,'second arg');
dbms_scheduler.enable('myjob');
end;
/
Use PLSQL_BLOCK instead:
begin
dbms_scheduler.create_job (
job_name => 'myjob',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN myproc(''first arg'',''second arg''); END;',
start_date => sysdate,
repeat_interval => 'FREQ=HOURLY',
enabled => true
);
end;
/
Related
Is it possible to have default values in arguments of Stored procedures of Snowflake. For the below example, I am getting error. Please help
syntax error line 1 at position 53 unexpected ''test''.
create or replace procedure test(arg1 string default 'test')
returns string not null
language sql
as
$$
begin
return arg1;
end;
$$
;
Snowflake's procedures applies polymorphism instead of using default value. This solution is when you do not want to call sp like func1(Null)
For example (sql scripting):
create or replace procedure func1(arg1 varchar, arg2 varchar)
...
create or replace procedure func1(arg1 varchar)
...
call func1(arg1 , 'some-default-value')
...
One option could be providing NULL as value and handle it at the begining of the stored procedure with COALSESCE:
create or replace procedure test(arg1 string)
returns string not null
language sql
as
$$
begin
arg1 := COALESCE(arg1, 'test');
return arg1;
end;
$$;
CALL test(NULL);
-- test
Setting a default value/values as arguments directly in Stored procedures is not available in Snowflake currently
The below link can be referred for the allowed syntax in Stored Procedures
https://docs.snowflake.com/en/sql-reference/sql/create-procedure.html#syntax
I am trying to accept user input inside of a stored procedure and assign it to a VARCHAR variable. For some reason I get error
PLS-00201: identifier 'userinput' must be declared.
Any ideas? I have to use this later down the line to see how many times the input appears in a table.
CREATE OR REPLACE PROCEDURE nums
AS
x_num VARCHAR(20);
BEGIN
x_num := &input;
dbms_output.put_line('You entered: ' || x_num);
END;
/
Procedures cannot receive user input in response to a prompt, PLSQL is NOT interactive. When you have that you are not actually communicating with the database. What is actually happening is symbol substitution were SQLPLUS or other interface (Toad , SQL Developer, ...) is actually intercepting symbol, requesting the input, and physically changing the script before submitting it to the database. If you want a stored procedure you will need to use a parameter as #HereGoes suggested and then provide the user a script as follows:
Begin
nums(pInput => &Input);
end ;
Or provide an application interface to receive the input value and call the procedure or allow user access through SQLPLUS or other interface and let them enter the script - not recommended.
I suggest making the input a parameter.
CREATE OR REPLACE PROCEDURE nums (pInput IN VARCHAR2)
AS
x_num VARCHAR(20);
BEGIN
x_num := pInput ;
dbms_output.put_line('You entered: ' || x_num);
END;
/
I want to be able to access the InputLocalEnvironment within a Procedure call.
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
SET OutputLocalEnvironment = InputLocalEnvironment;
--Call Procedure doStuff
END
CREATE PROCEDURE doStuff ( IN inputLocalEnvironment ) RETURNS BOOLEAN
BEGIN
--Do Stuff with the inputLocalEnvironment
END
What is the data type I should use to pass InputLocalEnvironment as the above procedure will obviously through an error.
CREATE PROCEDURE doStuff ( IN inputLocalEnvironment DataType)
If someone has a better suggestion I'm open to the idea but I need to be able to get information out of the local variables and then place them into an output.
Many thanks.
It should work with the REFERENCE data type:
CREATE PROCEDURE doStuff (IN inputLocalEnvironment REFERENCE)
Problem that I was facing is that I have to call one stored procedure from another stored procedure in DB2 database. Kind example that I am giving right below.
I have one stored procedure:
CREATE OR REPLACE PROCEDURE Proc1()
IS
Declare myName in varchar;
BEGIN
Select fname into myName from student where fname='x'; // is returning unique value
-- here call anoher proc2
END;
Now so this proc1 procedure is going to call this proc2 procedure.
Now I have second stored procedure:
CREATE OR REPLACE PROCEDURE Proc2(Name in varchar)
IS
BEGIN
-- do anything
END;
I solved this problem,
So solution is like If we want to execute proc using sql command then syntex is like below,
call Proc2('My Name');
We can use this same approach inside our proc also.
For that we have to follow some steps. Lets say that our above sql call is statement that we want to execute. we are going to convert that statement into String and pass necessary parameter by concating variable values. Then execute statement.
CREATE OR REPLACE PROCEDURE Proc1()
IS
Declare myName in varchar;
-- stmt variable is to execute our proc
STMT VARCHAR(4000);
BEGIN
Select fname into myName from student where fname='x'; // is returning unique value
-- this is our logic
STMT :='call Proc2('||myName||')';
EXECUTE IMMEDIATE STMT;
END;
Can a procedure PL/SQL take a variable number of arguments?
In my case, the procedure is called by the submit button of a form, and the form has variable number of inputs.
You don't mention it, but are you using mod_plsql?
If so, you should read about flexible parameter passing.
In short, prefix your procedure name with an exclamation mark in your browser and define your procedure with a name_array and value_array.
Sort of. You can give the procedure parameter default values:
CREATE PROCEDURE myproc( p_value_a NUMBER DEFAULT 1,
p_value_b NUMBER DEFAULT 2 ) AS
...
which you could call like this:
myproc( 999 );
or like this:
myproc (p_value_b => 11 );