9

I have a procedure like :

CREATE OR REPLACE PROCEDURE test is 
BEGIN

  DBMS_OUTPUT.PUT_LINE('This is a Test');
END;

I want to run some sql scripts stored in the current directory. I could run them from sqlplus with '@scriptname.sql' but how can i do it from inside the procedure ? For ex:

CREATE OR REPLACE PROCEDURE test is 
BEGIN

  DBMS_OUTPUT.PUT_LINE('This is a Test');
  @scriptname.sql

END;

This doesn't seem to work ! Is there a specific to run sql scripts from pl/sql procedures ?

2
  • Does make sense to me why you'd enclose script references in a stored procedure (or anonymous, for that matter) -- nevermind the issue with script location. You can run scripts from a single master script using SQLPlus, without the need for the stored procedure. Commented Jan 23, 2011 at 5:41
  • I would guess this is a gross simplification of OP's actual needs... Commented Jan 24, 2011 at 4:57

5 Answers 5

11

You can't, in general, because the pl/sql is run in the database, on the server, and sqlplus is a client process. The server can't rely on even being on the same system as the client and its files, much less knowing anything about how to find the file the client is referring to. Even if the syntax were supported (and it isn't), your sql script would have to be on the server, in a location the server knew about and had access to.

Sign up to request clarification or add additional context in comments.

Comments

8

Actually, you can do this in SQL*Plus - you just need to ensure the @ is the first character on the line, e.g.:

CREATE OR REPLACE PROCEDURE test is 
BEGIN

    DBMS_OUTPUT.PUT_LINE('This is a Test');
@scriptname.sql

END;

SQL*Plus will read the entire contents of the script and insert it at that point in the procedure, then create the procedure as it is given. That means you can't have SQL*Plus commands in scriptname.sql. Also, there won't be any reference to @scriptname.sql in the actual procedure created on the database.

Comments

4

You could execute an OS command to start SQLPlus and have that execute the scripts. You can pass a filename into SQLplus at start up and it will execute it.

Google External Procedures and extproc or this article. Or something like call OS command with Java

2 Comments

And it still runs on the server, not the client.
Actually running on the server or elsewhere is just one of the details that will have to be coded. I am certainly not saying it will be easy but if there it is possible to execute code on the server and cause it to execute elsewhere. There will be a mess of firewall, RPC (or similar) issues, etc to resolve but it is possible. It's not something I'd see much merit in but I don't know the requester's scenario.
1

You could write a Java Stored Procedure to open the file and return its contents as a String and then call Execute Immediate on the String.

Be VERY CAREFUL doing this though as any malicious sql in those files can do pretty much whatever it wants.

1 Comment

This will not work if the SQL file contains SELECT statements (Where should the selected data go to? Execute Immediate won't display it on the screen.) or SQLplus statements such as EXEC, SET ESC etc. Furthermore, you'll probably need to read and execute it statement by statement which isn't easy either.
1

Even if there should be a solution, I would not recommend to to this. A PL/SQL procedure basically is a SQL script. Either 1. run your SQL scripts from outside the database, e.g. via shell script or 2. move the SQL code inside your procedure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.