This question already has an answer here:
PL/SQL procedure succesfully completed but shows nothing
(1 answer)
Closed 8 years ago.
I have created package like this:
create or replace package test_package is
procedure ShowDate;
end test_package;
/
create or replace package body test_package is
PROCEDURE ShowDate
IS
begin
dbms_output.put_line(to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'));
END ShowDate;
end test_package;
/
I'd like to run this package using sqlplus and have result (SYSTDATE) stored in a log file.
I've created a file ShowDate.sql containing:
call test_package.showdate()
I've tried ro run in as:
sqlplus user/password#server
spool ShowDate.log
#ShwoDate.sql
spool out.
But the onlu result I can see is: Call completed.
I have also tried modification of ShowDate.sql :
begin
test_package.showdate;
end;
/
but then I get PL/SQL procedure successfully completed.
Can anyone help?
Regards
Pawel
You need this line in your SQL*Plus script (before the procedure call):
set serveroutput on
Related
I'm currently trying to implement a bash script that runs at the end of the day that runs a simple oracle query. The command works just fine in Oracle but when inside a .sql file it does not run.
I've attempted to put all of the code on one line and adding semicolons.
Contents of batch file (with user/pass altered):
sqlplus username/password#database #set_changed.sql
Contents of set_changed.sql file:
UPDATE ris_web a
SET a.changed = 0
where exists
(
select modified_date from invn_sbs b
where b.item_sid = a.item_sid
and b.modified_date <= sysdate-1
);
COMMIT;
END;
In SQLPlus you should use a / on a separate line to terminate an SQL statement or PL/SQL block instead of using a semicolon, so change your file to
UPDATE ris_web a
SET a.changed = 0
where exists
(
select modified_date from invn_sbs b
where b.item_sid = a.item_sid
and b.modified_date <= sysdate-1
)
/
SHOW ERRORS
/
COMMIT
/
SHOW ERRORS
/
END
You might also want to have a look at this question
You have end; command at the end of the script but no BEGIN for it.
Simply replace the END; with /
The original syntax was correct. There were uncommitted changes in sqldeveloper that were causing the .sql file to never finish running. Thank you everyone for your help.
This question already has answers here:
Calling another PL/SQL procedure within a procedure
(2 answers)
Closed 4 years ago.
I have looked it up already but couldn't find the right solution for my question. I have many Procedures which copy tables from one database Schema called 'source' into another Schema on the same database called 'target'. The Procedures themself work if I execute them individually. Now I want to execute them with only one command. The problem is I don't know how. I want to do that because there are many procedures and it is annoying to execute them all individually. It would be nice if someone could help me out. Thanks in advance :)
My approach (which is obviously wrong) was:
CREATE OR REPLACE PROCEDURE COPY_TABLES
IS
BEGIN
EXEC COPY_EAKTEPERSON;
EXEC COPY_EANDEREANSRPUECHE;
EXEC COPY_EANDEREBHBERECHT;
EXEC COPY_EBEIHILFEBEMSATZ;
EXEC COPY_EBESCHAEFTIGUNG;
EXEC COPY_EDIENSTSTELLE;
EXEC COPY_EEIGENBEHALT;
EXEC COPY_EPFLEGEVERS;
EXEC COPY_ESEHSCHAERFE;
EXEC COPY_EVERSLEISTUNG;
EXEC COPY_EWOHNISITZ;
EXEC COPY_EPERSON;
END;
A procedure calling another procedure.
create or replace procedure COPY_TABLES(
ret out varchar2) as
error varchar2(1000);
begin
COPY_EAKTEPERSON();
COPY_EANDEREANSRPUECHE();
COPY_EANDEREBHBERECHT();
COPY_EBEIHILFEBEMSATZ();
COPY_EBESCHAEFTIGUNG();
COPY_EDIENSTSTELLE();
COPY_EEIGENBEHALT();
COPY_EPFLEGEVERS();
COPY_ESEHSCHAERFE();
COPY_EVERSLEISTUNG();
COPY_EWOHNISITZ();
COPY_EPERSON();
ret := ''
return;
exception
when others then
error_info := sqlerrm;
ret := error_info;
end COPY_TABLES;
What is the problem with this package as it is giving an error?
CREATE OR REPLACE PACKAGE PKG_SHOW_CUST_DETAILS
AS
PROCEDURE SHOW_CUST_DETAILS( myArg VARCHAR2);
END PKG_SHOW_CUST_DETAILS;
CREATE OR REPLACE PACKAGE BODY PKG_SHOW_CUST_DETAILS
AS
PROCEDURE SHOW_CUST_DETAILS(myArg VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE(myArg);
END SHOW_CUST_DETAILS;
END PKG_SHOW_CUST_DETAILS;
/
On compilation of the above script, I am getting the following errors:
SQL> show errors
Errors for PACKAGE PKG_SHOW_CUST_DETAILS:
LINE/COL ERROR
-------- -----------------------------------------------------------------
6/1 PLS-00103: Encountered the symbol "CREATE"
The package is very simple and I am not able to compile it. I searched earlier answers on this error message and none of them did solve my problem.
I am consistently getting this error for 2 more packages and I am stuck on this error message no matter what I do. I even tried to strip everything to the barest minimum as shown above, but the error message does not seem to go away.
BTW I am executing this on command line SQL plus session after logging into my Oracle 11G database.
YES- SET SERVEROUTPUT ON -- is executed and the error message has nothing to do with this command.
What am I missing?
At line 5 there is a / missing.
There is a good answer on the differences between ; and / here.
Basically, when running a CREATE block via script, you need to use / to let SQLPlus know when the block ends, since a PL/SQL block can contain many instances of ;.
For me / had to be in a new line.
For example
create type emp_t;/
didn't work
but
create type emp_t;
/
worked.
In my case EXECUTE IMMEDIATE ('CREATE TABLE ...') works, for example:
DECLARE
myVar INT;
BEGIN
SELECT 2 INTO myVar FROM dual;
IF myVar > 1 THEN
EXECUTE IMMEDIATE('Create Global Temporary Table TestTemp ( id VARCHAR2(2) ) ON COMMIT PRESERVE ROWS');
END IF;
END;
Reference to Create Table As within PL/SQL?
Run package declaration and body separately.
i have lots of packages which needed to be compiled when i move from development to production or when we release a change request.
right now , we compile each of the packages one by one using toad or sqldbx , is there a way that i can write a batch file with sqlplus command so that i can run all my packages in one go.. like *.sql
You can execute dbms_utility.compile_schema(user,false); to compile all invalid objects in your schema at once.
You can read about that procedure here in the documentation: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e16760/d_util.htm#ARPLS73226
Regards,
Rob.
Normally when we do lots of changes in a database that invalidates lots of objects, the easiest way to get them recompiled is by running sqlplus "/ as sysdba" #?/rdbms/admin/utlrp This procedure gets smarter every release and from 10g it uses the Oracle Scheduler to work in parallel. This of course only works with dba access to the database. If you lack that Rob van Wijk's answer is the way to go.
You can put all the SQLs in a text file and execute that by:
SQL > #/path/script.sql
You just need to provide path of script to be executed.
My approach would be to copy all package scripts into a directory then create a single sql script in that directory to load all packages, see example below.
-- load package specifications
##package1.pks
##package2.pks
-- load package bodies
##package1.pkb
##package2.pkb
One way of tackling this is to deploy your code in the correct order.
PL/SQL packages themselves are the API for the code in the package body, and the packages themselves are not dependent on each other. Package bodies however can be dependent on packages, so if a package is recompiled than it runs the risk of invalidating package bodies that reference it.
Unfortunately it's very common to see deployments that work in this order:
create or replace Package A ...;
create or replace Package Body A ...;
create or replace Package B ...;
create or replace Package Body B ...;
create or replace Package C ...;
create or replace Package Body C ...;
This has the side-effect that if code in Package Body A is dependent on Package B, then when Package B is (re)created it invalidates Package Body A.
The correct sequence for deployment is:
create or replace Package A ...;
create or replace Package B ...;
create or replace Package C ...;
create or replace Package Body A ...;
create or replace Package Body B ...;
create or replace Package Body C ...;
If there have not been changes in the package itself then there is no need to deploy it at all, of course.
Respecting these methods should give you much fewer invalid objects.
Package Headers first:
for i in $(ls *.hed); do sqlplus user/password #$i; done
Then package bodies:
for i in $(ls *.hed); do sqlplus user/password #$i; done
you can use dba_objects to check for invalid objects and use dynamic sql to generate compile statements, something like:
select 'alter ' || object_type || ' ' || owner || '.' || object_name || ' compile;'
from dba_objects
where status = 'INVALID'
and object_type in ('PACKAGE', 'PROCEDURE', 'FUNCTION');
you can then put that into a sql script.
You can also look into utl_recomp package
I'm trying to update a package in Oracle, coming from SQL Server this has been confusing.
I have written a batch file that runs the .spec file first and the .body file second, but even running it manually does not work.
I use this syntax:
sqlplus username/password#databasename #c:\temp\myfile.spec
sqlplus username/password#databasename #c:\temp\myfile.body
When I go back to Sql Developer I can look at the stored procedures in the package and see that they have not been updated.
Why aren't my packages getting updated?
The spec and body files need to have / make SQL*Plus create/replace the object.
Without the /:
CREATE OR REPLACE PACKAGE TEST12_13 AS
PROCEDURE TEST12_13;
END;
STAGE#DB>#C:\TEST.PKS
6
With the /:
CREATE OR REPLACE PACKAGE TEST12_13 AS
PROCEDURE TEST12_13;
END;
/
STAGE#DB>#C:\TEST.PKS
Package created.
In reply to your comment about passing filename as parameter, instead of passing the filename as parameter, have SQL*Plus ask you for the filename
wrapper.sql
ACCEPT filename_var Prompt 'Enter filename'
#c:\temp\&filename_var
/
#c:\temp\&filename_var
/
Connect to SQL*Plus with
sqlplus username/password#databasename
Then run the script from the SQL*Plus prompt:
set echo on
#c:\temp\myfile.spec
You should be able to see whats going on like this, including any error messages.