How read a value in pl/sql procedure? - plsql

I am having problem regarding to scan a value in a pl/sql procedure. When I have execute the procedure, it ignores the a:='&a';.
Procedure Body
create or replace PROCEDURE Testing
IS
a VARCHAR2(3);
BEGIN
DBMS_OUTPUT.PUT_LINE('Enter a : ');
a:='&a';
END Testing;
Procedure Calling
SET SERVEROUTPUT ON;
cl scr;
Execute Testing;
Output
PL/SQL procedure successfully completed.
Enter a :
Can anybody help me, please!?

In SQL*Plus we have the prompt and accept syntax, but there is no direct way to make PL/SQL interactive without using PHP, Apex, Java, Scripting. I am giving you example of scripting.
e.g. In Windows batch, following code will help.
1) Code for the Testing procedure is same what you have
create or replace PROCEDURE Testing (arg varchar2)
IS
a VARCHAR2(3);
BEGIN
a:=arg;
DBMS_OUTPUT.PUT_LINE('Value of a : '||a);
END
2) test.bat
sqlplus nd211/nd211 #Testing.sql te1
sqlplus nd211/nd211 #Testing.sql te4
3) Testing.sql
set serveroutput on
exec Testing('&1');
exit;
Testing;
/
4) Sample output
Value of a : te1
PL/SQL procedure successfully completed.
Value of a : te4
PL/SQL procedure successfully completed.

Related

Executing procedure in plsql developer

I am totally new to PLSQL and I am struggling to execute a procedure in PLSQL Developer. I have created a procedure named as 'employee' as follows:
CREATE OR REPLACE PROCEDURE employee IS
var_name VARCHAR2(20) := 'Parkavi';
var_web VARCHAR2(20) := 'parkavi.com';
BEGIN
DBMS_OUTPUT.PUT_LINE('Hi! I am ' ||var_name|| 'from' ||var_web);
END employee;
Now I need to execute this procedure so that I can view the output. Please help me out. Thanks in advance!!
In PL/SQL Developer, you execute PL/SQL blocks in the Test Window.
File > New > Test Window will provide a template block something like this:
declare
begin
end;
You just need to add your procedure name (and remove the unneeded declare section as you have no variables), so it's:
begin
employee;
end;
Alternatively, right-click on the procedure name and select 'Test' from the pop-up menu, and it will generate the above block for you.
If the expected dbms_output text is not displayed in the 'Output' tab, the first thing to check is that the 'Enabled' checkbox is checked.
To diagnose dbms_output, the simplest test case would be just:
begin
dbms_output.put_line('Hello');
end;

Ending PLSQL procedure

I wrote a PLSQL procedure in Oracle APEX, but I don't know how to end it as every way I have tried it still complains.
DECLARE
PROCEDURE FzgZuordnen(Volt VARCHAR2) IS
Variable Declarations
*
BEGIN
*
END;
FzgZuordnen END;
I have also tried
*
END;
END;
But it doesn't seem to like any way I end my procedure. I have ended all of the things inside the procedure.
Thanks for any help.
You can declare a local procedure in a PL/SQL block anywhere - including in APEX. The syntax is like this:
DECLARE
PROCEDURE FzgZuordnen(Volt VARCHAR2) IS
-- Variable Declarations
BEGIN
-- Procedure code
END FzgZuordnen;
BEGIN
-- Block PL/SQL that calls the procedure
END;
For example:
DECLARE
PROCEDURE raise_error (error_text VARCHAR2) IS
BEGIN
raise_application_error (-20001, error_text);
END FzgZuordnen;
BEGIN
if :p1_value < 0 then
raise_error ('Value cannot be negative');
elsif :p1_value > 10 then
raise_error ('Value cannot exceed 10');
end if;
END;
Because the procedure is declared locally it can only be used from the PL/SQL block where it is declared. If you needed a procedure that could be called from many placed in your application or page then it would need to be defined in the database (preferably in a package).
To my knowledge you cannot place PL/SQL procedures directly in an APEX page. For PL/SQL execution there are APEX processes. If you have to re-use your code on other pages then either copy the process to each page or use a db procedure (meaning putting the procedure directly in the database)
Using APEX process:
Create a new Process on the Page and select "PL/SQL Code" as type. Then type your Code into the APEX process.
Variable Declarations
*
BEGIN
*
END;
Processes can only be used inside the same page. If you want to use your code on multiple other pages you have to copy them to this page.
Using DB-procedure:
If you use a db procedure you have to add it directly to the oracle database. Therefor connect to the database with a tool of your choice. Execute your code from above. To call the procedure in APEX use something like this.
begin
PKG.FzgZuordnen(:APEX_PAGEITEM);
end;
try this way:
DECLARE
PROCEDURE FzgZuordnen(Volt VARCHAR2) IS
Variable Declarations
*
BEGIN
*
END FzgZuordnen;
BEGIN
*
END;
You can create a plsql procedure inside a database schema using sql developer or sql plus
create or replace procedure zgZuordnen(Volt VARCHAR2) IS
.. variable declaration
begin
{statament blocks}
end zgZuordnen;
From APEX frontend developer option you can call the procedure with bind variable

Pass parameter to spool file

Say I have a sql file which is executed from command prompt (SQL Plus), which is working fine.
sqlplus dbusername/dbpassword#DBInstance #sqlfilename.sql
Now I need to pass a parameter to this sql spool file and according to the value of parameter I should be able to determine the functionality in the sql file. In the below sql code, I need to store the value that I am passing as parameter and in the variable parameter_Value
SET SERVEROUTPUT ON
SET DEFINE OFF
SPOOL "Test.log"
var parameter_Value VARCHAR2(20);
BEGIN
IF(parameter_Value='A')
THEN
--do something
ELSE
--do something
END IF;
END;
/
SPOOL OFF;
SET DEFINE ON
SET SERVEROUTPUT OFF
Can anyone please help how to perform this task?
Thanks in advance!
Your usage of the word spoolfile is not correct. sqlfilename.sql is the sql file or sqlplus file. The output file Test.log is the spool file.
you call
sqlplus dbusername/dbpassword#DBInstance #sqlfilename.sql A
to call the following file
SPOOL "Test.log"
DEFINE MY_VALUE=&1
SET SERVEROUTPUT ON
var parameter_Value VARCHAR2(20);
EXEC :parameter_Value:='&MY_VALUE'
SET DEFINE OFF
BEGIN
IF(:parameter_Value='A')
THEN
--do something
ELSE
--do something
END IF;
END;
/
SET DEFINE ON
SET SERVEROUTPUT OFF
SPOOL OFF;
the substitution of the & variables is a simple text substitution. It can be turned off by executing SET DEFINE OFF. The substituion raises information messages by sqlplus that can be turned off by executing SET VERIFY OFF
you should start with the SPOOL command, you won't see errors thrown by commands executed before you start spooling into the logfile. This can make debugging tedious. Also you should execute all commands before SPOOL OFF
the parameters in the command line are referend by their position number. I prefer assigning them to a named variable and use the named variable later. This is not necessary. You can use the positional parameter later, too, instead of using a named parameter. But using named parameter makes it easier to change the file if the position of a parameter changes. Also it can document the purpose of the parameter if you choose an appropriate name. I didn't in this example.
You cannot disable the special property of the & character with SET DEFINE OFF before the parameter substitution. You have to postpone it until you have done all your substitutions. Otherwise the substitution will not work.
a bind variable is definded with the VAR ... statement in sqlplus. In an SQL text it must be referenced with a preceding colon (:).
EXEC statement is an abbreviation for BEGIN statement; END
be aware that the --do something is in a PL/SQL block. So it must be replaced by PL/SQL statements and not by SQL statements or SQL*Plus statements.
So it cannot be replaced by a CREATE TABLE ... statement (this is SQL but not PL/SQL) and it cannot be replaced by a SPOOL OFF statement, which is SQL*Plus but not PL/SQL.

Executing a variable in oracle

The below procedure compiled successfully. But, when I try to run the its getting error.
CREATE OR REPLACE PROCEDURE SAMPLE_PROCEDURE
AS
VARIABLE1 VARCHAR2(2000);
BEGIN
VARIABLE1:='DECLARE A TIMESTAMP:=LOCALTIMESTAMP;
CREATE GLOBAL TEMPORARY TABLE TEMP_BWXROW
(ROW_ID NUMBER(10),DIVISION VARCHAR2(256),OUTLET VARCHAR2(256),CLASS VARCHAR2(256));';
EXECUTE IMMEDIATE VARIABLE1;
END;
Error is:
6550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Can you help me, what is wrong in this.
Very strange to be dynamically creating a global temporary table. Anyway, I assume this is all part of a learning exercise ...
Try this:
CREATE OR REPLACE PROCEDURE SAMPLE_PROCEDURE
AS
VARIABLE1 VARCHAR2(2000);
BEGIN
VARIABLE1:='DECLARE A TIMESTAMP:=LOCALTIMESTAMP;
BEGIN
CREATE GLOBAL TEMPORARY TABLE TEMP_BWXROW
(ROW_ID NUMBER(10),
DIVISION VARCHAR2(256),
OUTLET VARCHAR2(256),
CLASS VARCHAR2(256));
END';
EXECUTE IMMEDIATE VARIABLE1;
END;
What's wrong with this is that you almost certainly shouldn't be creating a table at runtime and you almost certainly shouldn't be using dynamic PL/SQL and you really, really, really shouldn't be creating a table dynamically inside a dynamic PL/SQL block.
If you are absolutely determined to do so, the CREATE TABLE statement would itself need to use dynamic SQL inside of the dynamic PL/SQL block. If I've got all my quotes escaped correctly, you'd end up with something like this
CREATE OR REPLACE PROCEDURE SAMPLE_PROCEDURE
AS
VARIABLE1 VARCHAR2(2000);
BEGIN
VARIABLE1:='DECLARE
A TIMESTAMP:=LOCALTIMESTAMP;
BEGIN
EXECUTE IMMEDIATE ''CREATE GLOBAL TEMPORARY TABLE TEMP_BWXROW
(ROW_ID NUMBER(10),
DIVISION VARCHAR2(256),
OUTLET VARCHAR2(256),
CLASS VARCHAR2(256))'';
END;';
EXECUTE IMMEDIATE VARIABLE1;
END;
If I came across a piece of code doing this in one of my systems, though, I would have a very strongly worded conversation with the original developer to understand what possessed them to believe that any problem necessitated PL/SQL executing a dynamic PL/SQL block that itself executed a dynamic SQL statement.

how do you spool from a stored procedure that is executed through a database link?

I am using a UNIX script to run sql code that kicks off a stored procedure via database link. I can get the procedure to complete successfully, however none of the DBMS outputs are spooled to the SPOOL file indicated.
SQL within UNIX:
set feedback off;
set linesize 500;
set serveroutput on size 1000000;
set serveroutput on format wrapped;
spool $SQLspool;
whenever oserror exit;
whenever sqlerror exit sql.sqlcode;
DECLARE
retcode integer :=0;
BEGIN
owner.procedure#db;
dbms_output.put_line('');
dbms_output.put_line('return code: ' || retcode);
dbms_output.put_line('');
EXCEPTION
WHEN OTHERS THEN
RAISE;
END;
/
EXIT;
SPOOLFILE CONTENTS:
return code: 0
I list a bunch of DMBS outputs within the stored procedure but nothing is written to the spool file.
How can I get it to output to the spool file?
I tried to have IN OUT variables, but because the procedure contains COMMITs it errors out with the parameters since it is going through the DB Link...
The output for PUT and PUT_LINE is buffered. From the Oracle docs
:
SQL*Plus does not display DBMS_OUTPUT messages until the PL/SQL program completes. There is no mechanism for flushing the DBMS_OUTPUT buffers within the PL/SQL program.
So, if you are looking to stream responses you are going to need to write a small program which does not buffer the output.

Resources