PL/SQL Code Validation without execution [duplicate] - oracle

This question already has answers here:
Parsing PL/SQL code to check against syntax and semantic errors
(4 answers)
Closed 6 years ago.
I would like to be able to validate that a piece of code is correct using only PL/SQL.
For example, I have the following code in a variable, I would like to validate it:
my_code:='BEGIN
package1.get_wordlist_keywords_PG;
package1.get_wordlist_keyword_cat_PG;
END;
'
I would like to do something like:
result:=validate_code(my_code);
and result would be "valid" or "invalid" (or with error message etc.)
I am sure this would be possible, as Oracle Apex allows users to enter PL/SQL and runs a validation on the code when it is saved.
I am on Oracle 12c.
Thanks!
P.S. - I do not need to validate a simple sql select, it needs to be a PL/SQL code block.

Probably put your code in a procedure instead of anonymous block.
CREATE OR REPLACE PROCEDURE p1 AS
BEGIN
package1.get_wordlist_keywords_PG;
package1.get_wordlist_keyword_cat_PG;
END;
/
show errors;
After the execution, the procedure may or may not be created successfully. Though the procedure is created successfully, the code inside the procedure
is not executed.

CREATE OR REPLACE PROCEDURE VALIDATE_PLSQL
(
PLSQL IN VARCHAR2
, MY_RESULT OUT VARCHAR2
) AS
my_code varchar2(31900);
validate_code varchar2(32000);
status varchar2(4000);
BEGIN
my_code:=PLSQL;
validate_code:='create or replace procedure testp1 AS '||my_code;
BEGIN
status:='PASSED';
EXECUTE IMMEDIATE validate_code;
exception
when OTHERS then
status:='FAILED';
END;
EXECUTE IMMEDIATE 'drop procedure testp1';
MY_RESULT:=status;
END VALIDATE_PLSQL;
Pass in the block to be validated in "PLSQL" get the results in "MY_RESULT"
Clunky, but works!

Related

How to use Substitution in Stored Procedure

Actually I am creating stored procedure with substitution, while trying to compile the procedure, I get the popup to enter the substitution values in compiling itself, Instead of getting popup while execution.
Please share me your idea to compile the procedure without asking the substitution
In SQL*Plus or Oracle SQL Developer, you'd SET DEFINE OFF. You tagged the question with PL/SQL Developer tag (which is a tool I don't use), but - see if this helps.
However: I'd suggest you not to do it that way. If you're creating a stored procedure, then use its parameters, don't ask for substitution variables. Something like this:
SQL> set serveroutput on
SQL> create or replace procedure p_test (par_deptno in dept.deptno%type) is
2 begin
3 dbms_output.put_line('Department ' || par_deptno);
4 end;
5 /
Procedure created.
SQL> exec p_test(10);
Department 10
PL/SQL procedure successfully completed.
You can now reuse such a procedure, passing any parameter value you want.
The way you're doing it now:
SQL> create or replace procedure p_test is
2 begin
3 dbms_output.put_line('Department ' || &par_deptno);
4 end;
5 /
Enter value for par_deptno: 25
old 3: dbms_output.put_line('Department ' || &par_deptno);
new 3: dbms_output.put_line('Department ' || 25);
Procedure created.
SQL> exec p_test
Department 25
PL/SQL procedure successfully completed.
SQL>
you can run the procedure many times, but it'll always display (i.e. use) the same value, throughout that session. Once you exit and log in again, procedure will always use the same value.
You cannot! A substitution variable acts like a find-replace operation in the client application at the time the statement is run; the database does NOT see the substitution variable as the client application you are using will have already performed the find-replace operation. Which, for a procedure, would be at the time the CREATE PROCEDURE statement is sent from the client to the database to be compiled. It is NOT an operation that the database performs.
If you try it in a client that does not support substitution variables (or in a client that does support it after turning off substitution variables using, for example, the command SET DEFINE OFF in the client application) then you will get a compilation error. db<>fiddle
If you want to use substitution variables then use them in the anonymous block when you call the procedure.
An example procedure would take parameters and have no substitution variable:
CREATE PROCEDURE procedure_name(
p_value IN NUMBER
)
IS
BEGIN
-- Do something
DBMS_OUTPUT.PUT_LINE( p_value );
END;
/
Then when you want to execute the procedure you can use a substitution variable in the calling block:
BEGIN
procedure_name( &value );
END;
/

Calling another PL/SQL procedure within a procedure

I'm new to PL/SQL & would greatly appreciate help in this. I've created a procedure to copy contracts. Now I want to call another procedure from within this procedure which shall copy all the programs related to the contract I'm copying. One contract can have multiple programs.
You cal call another procedure in another package by using PackageName.ProcedureName(vcParameters => 'InputParameter1'); If the procedure is in the same package you could do it without the PackageName, so just ProcedureName(vcParameters => 'InputParameter1');
You call a procedure by simply putting its name and parameters in your code, e.g.
begin
dbms_output.put_line('Demo');
end;
or within a procedure,
create or replace procedure demo
as
begin
dbms_output.put_line('Demo');
end;
I have used dbms_output.put_line as an example of a procedure, but obviously any other procedure would be called the same way:
begin
foo;
bar(1);
demo(true, 'Bananas', date '2018-01-01');
end;
For some reason, many beginners are tempted to add exec before the procedure call. I don't know where that comes from because PL/SQL has no such keyword. Possibly they are thinking of the SQL*Plus execute command, which can be abbreviated to exec. However, SQL*Plus is a separate command line utility with its own commands that have nothing to do with the PL/SQL language.

How do I pass a cursor value when calling a stored procedure?

This is only my second time diagnosing a PL/SQL procedure. I need to test the code in the stored procedure, and I'm trying to call it in SQL Developer. I have run the error details report, and the code has no obvious bugs in it.
So now I am trying to run it through a test window so I can see if the output is correct. However I can't seem to get the right argument for the 3 parameter. Here are the parameters in the the procedure.
CREATE OR REPLACE PROCEDURE ADVANCE.WW_DEATHDATE_REPORT(begindate varchar2, enddatevarchar2, RC1 IN OUT du_refCUR_PKG.RC) AS
Here is the Code I am trying to use to call the procedure. What do I need to do to get it to run correct? I keep getting error messages saying I'm using a wrong value in the parameter.
BEGIN
ADVANCE.WW_DEATHDATE_REPORT('20100101','20150101',du_refcur_pkg);
END;
There are multiple ways to do this, one way is like the below,
DECLARE
du_refcur_pkg SYS_REFCURSOR;
BEGIN
OPEN du_refcur_pkg FOR SELECT ... ;
ADVANCE.WW_DEATHDATE_REPORT('20100101','20150101',du_refcur_pkg);
END;
Another way would be,
BEGIN
ADVANCE.WW_DEATHDATE_REPORT( '20100101','20150101', CURSOR (SELECT ... ) );
END;

How to execute procedure in APEX SQL script?

I am trying to understand how to use multiple procedures in APEX SQL script. First I don't really need stored procedure, but not sure how to declare simple procedure in APEX SQL script. So this is my attempt:
create or replace procedure test1 as
begin
DBMS_OUTPUT.ENABLE;
dbms_output.put_line('test1');
end;
execute test1;
This gives me an error:
Error at line 7: PLS-00103: Encountered the symbol "EXECUTE"
So questions - how to create regular/not stored/ procedures in one SQL script and then call them. What is the entry point of execution in APEX SQL script?
UPD (At the first time I understood question totally wrong)
Correct version of a script:
create or replace procedure test1 as
begin
DBMS_OUTPUT.ENABLE;
dbms_output.put_line('test1');
end;
/
begin
test1;
end;
/
Documentation says, that script can contain inly SQL and PL/SQL commands. Commands of sqlplus will be ignored.
OLD VERSION (Let stay here)
In APEX pages you can use PL/SQL anonymous blocks. For example, you can create process (APEX has some types of them) or PL/SQL region, and use following:
declare
...
begin
some_proc(:P_MY_ITEM);
end;
Here you can invoke any procedure and do anything else that allowed by PL/SQL. Also you can use parameters like :P_ITEM_NAME to get and set values of page and application items.

oracle plsql: retrieve runtime parameter values when you call a procedure

I need a generalized method to get list of runtime parameters (values) when I call a procedure. I need something similar to the $$PLSQL_UNIT that returns the name of the running procedure.
(plsql Oracle 10g)
E.g. look at this sample procedure:
(it simply prints its own name and parameters )
CREATE OR REPLACE PROCEDURE MY_PROC(ow in varchar2, tn IN varchar2)
IS
BEGIN
dbms_output.put_line('proc_name: '||$$PLSQL_UNIT||' parameters: '|| ow||' '||tn );
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERRORE: ' ||SQLERRM);
END MY_PROC;
/
Running procedure produces the following output:
SQL>
1 BEGIN
2 IBAD_OWN.MY_PROC('first_par', 'second_par');
3 END;
4 /
proc_name: MY_PROC parameters: first_par second_par
PL/SQL procedure successfully completed.
I'm not satisfy because I can't copy and paste in all my procedures because I have to hard code each procedure to set their right parameter variables.
Thanks in advance for the help.
It isn't possible to dynamically retrieve the values of parameters passed to a procedure in Oracle PL/SQL. The language simply isn't designed to handle this kind of operation.
Incidentally, in a procedure that is located within a package, $$PLSQL_UNIT will only return the package's name. I find it's better to define a consistently-named constant within each procedure that contains the procedure's name.
When I wanted the same functionality as yours I didn't find any good built-in solution.
What I did is: wrote DB-level trigger which modifies original body of function/procedure/package.
This trigger adds immediatly after "begin" dynamically generated piece of code from "user_arguments".
Plus, after that I include into this trigger the code, that logs calls of procs when exception occures.
Plus, you can trace procs calls, and many more interisting things.
But this solution works fine only for preproduction because performance decreases dramatically.
PS. Sorry for my bad English.

Resources