Unable to put a WITH FUNCTION clause in a BEGIN/END block - plsql

Why doesn't the below code compile:
DECLARE
c number;
BEGIN
WITH
FUNCTION calculate(i IN NUMBER) RETURN NUMBER
AS
r number;
BEGIN
r := i*i;
RETURN r;
END;
select calculate(1) INTO c from dual;
END;
giving the following error:
Error report -
*ORA-06550: line 5, column 10:
PL/SQL: ORA-00905: missing keyword
ORA-06550: line 4, column 1:
PL/SQL: SQL Statement ignored
whereas:
WITH
FUNCTION calculate(i IN NUMBER) RETURN NUMBER
AS
r number;
BEGIN
r := i*i;
RETURN r;
END;
select calculate(1) from dual;
compiles?
Oracle version information
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production

This construct just doesn't seem to be supported in PL/SQL yet. Presumably it will be added in a future release.
In the meantime it's unpleasant but you could use dynamic SQL, which continues to run your working statement in a SQL context where it is understood:
DECLARE
c number;
BEGIN
EXECUTE IMMEDIATE '
WITH
FUNCTION calculate(i IN NUMBER) RETURN NUMBER
AS
r number;
BEGIN
r := i*i;
RETURN r;
END;
select calculate(2) from dual'
INTO c;
DBMS_OUTPUT.PUT_LINE(c);
END;
/
4
The documentation for select into doesn't show that the with clause is supported in PL/SQL even for subquery blocks, but that does work even in earlier releases. So it doesn't refer to the new PL/SQL declaration syntax either. Based on experiments in Oracle's Live SQL platform, which is running 12.2.0.1, it isn't supported in 12cR2 either.

Related

Procedure PL/SQL without parameters

I want to create a procedure without parameters (In SQL DEVELOPER) but I am not sure how to do it, I have tried it in the following way and it sends me an error in "Num1 NUMBER";
create or replace package PK_MAC as
PROCEDURE PR_PRUEBAS
IS
Num1 NUMBER;
BEGIN
Num1 := 2;
end;
end;
You're trying to create a procedure or a package, with a procedure?
Here is a working example of what you're doing, as a package.
Your package will have two parts, a SPEC and a BODY.
The SPEC will publicly share the definition of the procedure and the variable, NUM1.
The BODY will define what the procedure actually does. Since NUM1 is defined already in the context of the package in the spec, I can use it in my procedure in the body.
create or replace package PK_MAC as
num1 integer;
PROCEDURE PR_PRUEBAS;
end;
/
create or replace package body PK_MAC IS
procedure pr_pruebas is
BEGIN
Num1 := 2;
end pr_pruebas;
end PK_MAC;
/

Call another script inside PL/SQL

I am trying to call a variable script name. I need it as a variable so that i can replace it with a parameter. This code below somehow dose not work.
DECLARE
mypath CHAR(30);
BEGIN
mypath := '&1';
#mypath
END;
Any help please!
PL/SQL scripts is a subset of Sqlplus scripts. In other words you cannot run PROMPT hello from inside of PL/SQL block.
Although there's a way to run pure PL/SQL script stored in a file from PL/SQL block, I don't recommend it, because with a little thinking you're able to find more clear solution.
Anyway here's that dirty hack:
my/lovely/little_script_1.sql
BEGIN
dbms_output.put_line('hello from little_script_1');
END;
--------------------
my/lovely/little_script_2.sql
BEGIN
dbms_output.put_line('hello from little_script_2');
END;
--------------------
#main.sql
DECLARE
v_file_contents varchar2(32767) := q'[
#&1
]';
BEGIN
EXECUTE IMMEDIATE v_file_contents
END;
/
--------------------
#run.sql
set serveroutput on
#main.sql my/lovely/little_script_1.sql
#main.sql my/lovely/little_script_2.sql
--------------------
sqlplus user/pass#DB #run.sql
Notice, that little_script* files are pure PL/SQL, particularly they lack the / symbol in the end.
It's important to understand that construct #file_name.sql inlines file contents into current script if only # is the first non-space character in the line regardless of where this construct is placed. Also it takes place even inside PL/SQL - it's an undocumented feature (or a bug which never will be fixed).
So an implicit result of substitution #main.sql my/lovely/little_script_1.sql will be:
DECLARE
v_file_contents varchar2(32767) := q'[
BEGIN
dbms_output.put_line('hello from little_script_1');
END;
]';
BEGIN
EXECUTE IMMEDIATE v_file_contents
END;
Since file contents are just being inlined as is, mind handling quotes correctly.
For instance, if little_script_1.sql contains anywhere a string terminator symbols ]', then main.sql will face a syntactic error - try it yourself.
echo on
set lines 200 pages 999 trims on echo on feed on timing on time on
spool '&1'
#'&2'
spool off
spool '&1' APPEND
DECLARE
myoption CHAR(1);
BEGIN
myoption := '&Do_you_want_to_commit_y_n';
if myoption = 'y' thenQ
commit;
else
rollback;
end if;
END;
/
spool off
set echo off
exit;

PLS-00103 error when creating object type

I am trying to create and assign variables using following code to create object types in plsql (11g) but facing some errors:
begin
execute immediate 'drop type picu_obj force';
execute immediate 'drop type picu_obj_tab force';
execute immediate 'create type picu_obj as object(Customer_ID varchar2(32767),Customer_Name varchar2(32767),Server_Name varchar2(32767),Time_stamp varchar2(32767))';
execute immediate 'create type picu_obj_tab is table of picu_obj;';
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
end;
The above code gives following errors:
ERROR at line 6:
ORA-06550: line 6, column 10:
PLS-00103: Encountered the symbol "PICU_OBJ_TAB" when expecting one of the
following:
:= . ( # % ;
The symbol ":=" was substituted for "PICU_OBJ_TAB" to continue.
Please suggest what I am doing wrong here.
There are two problems with this code:
First: In Oracle 11g you can not use varchar2(32767) the maximum length is 4000 for a varchar there. So even if the code did run, it wouldn't create the types.
Secondly: the PL/SQL code is validated/compiled when you run it. But as you use dynamic SQL to create the types, the PL/SQL compiler can't see those types when it tries to compile the lines:
picu_var picu_obj_tab;
picu_var := picu_obj_tab(picu_obj('101','xyz','pro-ssr-qr','12:13'));
and that's the error you are seeing.
You have to create the types before you run PL/SQL code that uses them.

Error "BIND VARIABLE "A" NOT DECLARED" when running PL/SQL block

When running the following code in Oracle 10g (pl/sql)
DECLARE
A NUMBER;
B NUMBER;
BEGIN:
A:=&N;
B:=&M;
IF (A>B)
DBMS_OUTPUT.PUT_LINE('THE MAXIMUM OF TWO NUMBER IS:' || TO_CHAR(A));
ELSE
DBMS_OUTPUT.PUT_LINE('THE MAXIMUM OF TWO NUMBERS IS:' || TO_CHAR(B));
END IF;
END;
I get the error 'BIND VARIABLE "A" NOT DECLARED' and I don't know why. What is causing this?
There are a couple of things wrong with your code.
The first problem is that there should be no colon after BEGIN. What you've written is being interpreted by Oracle as BEGIN :A := ..., and that should explain why you're getting an error about bind variable A.
The second problem is with the line IF (A>B). You need to add a THEN to the end.
Incidentally, you can use GREATEST(A, B) to return the larger of two numbers.

PL/SQL script giving errors

I've to work on some older EDW scripts, which I think are in PL/SQL and queries fech data from Oracle table as well. But there is some problem with them, the part which declares variables, as shown in the image gives error. I'm unable to understand why?
Below is some part of script,
VARIABLE begin_exp_date varchar2(8)
VARIABLE end_exp_date varchar2(8)
VARIABLE begin_cal_key number
Declare
begin
:begin_exp_date := 'begin_exp_date';
:end_exp_date := 'end_exp_date';
:begin_cal_key := 'begin_cal_key';
end;
These lines produce error ORA-00900: Invalid SQL statement.
Any help?
If you plug the script in SQL*Plus, it will be executed without the ORA-00900 error. I guess you received the error when it was run in Toad.
If it is indeed PL/SQL, it should be more like this
DECLARE
begin_exp_date varchar2(8);
end_exp_date varchar2(8);
begin_cal_key number;
BEGIN
begin_exp_date := 'begin_exp_date';
-- and so on
END;
You can set constant values to the variables in the DECLARE section if you want. Note that you've defined begin_cal_key as a NUMBER so cannot assign the string 'begin_cal_key'

Resources