Running sql commands from a .sql file

Hello , I have this problem. How do i execute sql commands found in a .sql file with jdbc. My RDBMS is mysql.

You will have to open and parse the .sql file, connect to the database, and execute the statements. You can't simply tell JDBC to run your .sql file.
Depending on the complexity of your .sql file this is easy or hard. If its a bunch of e.g. updates or deletes or something you may be able to get away with running it as a block. In Oracle for example, you can put "begin" and "end;" around a bunch of statements and run it all together. In that case your program would open the SQL file, read the text, put it inside a "begin" and "end;", and run it as a single SQL statement.
If its a bunch of statements that must be run independently then you'll have to programatically split them up to run one at a time. If its a select and you want to e.g. print results to the screen, well, then it gets tougher...
- Jemiah

Similar Messages

  • Running sqlldr command from PL/SQL Block

    DECLARE
    BEGIN
    END;

    In SQL * plus we can run DOS commands using the following command
    HOST DIR
    HOST DIR/P
    But When we can't run the HOST command in PL/SQL Block..
    I have to Run sqlldr command from PL/SQL Block..
    i tried as follows
    DECLARE
    BEGIN
    EXECUTE IMMEDIATE ' host sqlldr control= bad= ';
    END;
    By
    BalaNagaRaju

  • How to run OS commands from PL/SQL???

    Hi
    Is there any way to run OS commands(Windows Platform) from within PL/SQL?
    Thanks

    APC,
    I am working on Discoverer, having lots of BAs & Workbooks created & stored in Database. Now if i want to move all the workbooks from one place to another (like from development to test or production) i can use Command line interface of Disco to do this. But i don't see any option there to export all the workbooks, so i thought of writing a pl/sql to get all the workbook names from the EUL then fire export Command for each record(although not a good practice but its a one time work). This is the place where i need to run OS command from within PL/SQL. Although it doesn't seem possible now(as you all said) without external procedure.
    Lastly, you are right that "Oracle make databases not operating systems" but my first impression/comment about oracle is:
    "Oracle is far Bigger, Powerful, Complex, Vast and Interesting System than any other..." so i believe it can do/capable doing anything :-)
    thx Ashutosh,
    Host is definitely an option but i can't run it from pl/sql(that i am looking for)..

  • Run OS command from Pl/SQL

    Is there any way to execute an OS command from pl/sql other than using dbms_pipe & pro*C combo. I have done this way before but it seems there is built-in oracle package which can do this in 8/8i.
    Also HOST command in forms will execute the OS coomand from the clinet. Is there any way to execute the OS command on the server from the forms clinet?
    thanks,
    Ravi.
    null

    Ravi,
    Can you send me an example how one can
    execute a this command from a PL/SQL
    program 'sed 3q /etc/passwd'.
    Please write to me at
    [email protected]
    thanks
    Pramod

  • Running linux commands from pl/sql

    Hi, I'm using a java class to run os commands but it doesn't work, i think it's permissions but i cant understand why.
    the java class is the following:
    import java.io.*;
    import java.lang.*;
    public class RunCmd extends Object{
    public static int RunThis(String args){
    Runtime rt = Runtime.getRuntime();
    int rc = -1;
    try{
    Process p = rt.exec(args);
    int bufSize = 4096;
    BufferedInputStream bis =
    new BufferedInputStream(p.getInputStream(), bufSize);
    int len;
    byte buffer[] = new byte[bufSize];
    while ((len = bis.read(buffer, 0, bufSize)) != -1)
    System.out.write(buffer, 0, len);
    rc = p.waitFor();
    catch (Exception e)
    e.printStackTrace();
    rc = -1;
    finally
    return rc;
    the pl/sql function i created:
    function run_cmd(p_cmd in varchar2) return number as language java name 'RunCmd.RunThis(java.lang.String) return oracle.sql.NUMBER';
    i gave the following permissions:
    begin
    dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'execute');
    dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'write');
    dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'read');
    dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'delete');
    dbms_java.grant_permission(user, 'SYS:java.lang.RuntimePermission', '*', 'readFileDescriptor');
    dbms_java.grant_permission(user, 'SYS:java.lang.RuntimePermission', '*', 'writeFileDescriptor');
    Dbms_Java.grant_Permission(user, 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
    Dbms_Java.grant_Permission(user, 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
    dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'execute');
    dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'write');
    dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'read');
    dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'delete');
    dbms_java.grant_permission(user, 'java.lang.RuntimePermission', '*', 'readFileDescriptor');
    dbms_java.grant_permission(user, 'java.lang.RuntimePermission', '*', 'writeFileDescriptor' );
    Dbms_Java.grant_Permission(user, 'java.lang.RuntimePermission', 'readFileDescriptor', '');
    Dbms_Java.grant_Permission(user, 'java.lang.RuntimePermission', 'writeFileDescriptor', '');
    commit;
    end;
    The command i'm trying to execute is:
    select run_cmd('/bin/sh -c "ls -ltr /home/mails> /home/mails/teste.txt"') from dual;
    i gave chmod 777 /home/mails and i already run this command directly in console mode and works just fine, so i dont think it's permissions related to the user oracle.
    when i run
    select run_cmd('/bin/sh -c "ls -ltr /home/mails> /home/mails/teste.txt"') from dual;
    it returns 2
    if i run
    select run_cmd('/bin/sh -c ls -ltr /home/mails> /home/mails/teste.txt') from dual;
    it returns 127
    if i run just
    select run_cmd('ls -ltr /home/mails> /home/mails/teste.txt') from dual;
    it returns -1
    if anyone as any clue please, i'm trying to solve this problem for 2 days and i don't have any clue for the solution...

    when i remove the /bin/sh -c it returns -1
    i think that it runs the command because there is a diference in the return value from a valid command a an invalid, for example:
    if i run a valid command
    select k_cs_controlo.run_cmd('/bin/sh -c "ls -ltr /home/mails > /home/mails/teste.txt"') from dual;
    it retuns 2
    if i run an invalid command(removed the " between the ls command)
    select k_cs_controlo.run_cmd('/bin/sh -c ls -ltr /home/mails > /home/mails/teste.txt') from dual
    it returns 127
    thanks for the replies Sven

  • Running OS command from PL/SQL

    Hi All,
    Is there any way of running a Operating System command thru the PL/SQL Procedure. I am using oracle on the Unix Platform. Do help me.
    Thanks In advance,
    Nitin Jain

    As one who did it, here's what I recommend.
    declare
    cmdstr varchar2(200); -- this is to compose the OS command to run
    begin
    -- then you should have something like this
    cmdstr:=...;
    cmdstr:=cmdstr| |...;
    host(cmdstr); -- this actually runs the command held in the cmdstr varchar
    end;
    Beware: you must be certain which OS you have to deal with, for some procs may be run on UNIX machines as well as on WINDOZE!

  • Running OS Command from PL/SQL: ORA-29532

    Hello All,
    I have created a java program, loaded into database using loadjava utility and finally gave grant on execute to public. Then made a wrapper function to call it inside as below:
       FUNCTION fn_OSCommand_Execute (command IN STRING)
          RETURN NUMBER
       IS
          LANGUAGE JAVA
          NAME 'OSCommand.Execute(java.lang.String) return java.lang.int';
    Logged into SYS and gave the following gants to SCOTT using dbms_java
    declare
    v_key NUMBER;
    begin
    DBMS_JAVA.GRANT_PERMISSION('SCOTT', 'SYS:java.io.FilePermission', '<<ALL_FILES>>', 'read,write,execute,delete',v_key);
    DBMS_JAVA.GRANT_PERMISSION('SCOTT', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '',v_key);
    DBMS_JAVA.GRANT_PERMISSION('SCOTT', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '',v_key);
    dbms_java.grant_permission( 'SCOTT', 'SYS:java.io.FilePermission', 'c:\windows\system32\cmd.exe', 'execute',v_key);
    COMMIT;
    end;
    When I am calling the function, I am getting the below error:
    ORA-29532: Java call terminated by uncaught Java exception: java.security.AccessControlException: the Permission (java.io.FilePermission c:\windows\system32\cmd.exe execute) has not been granted to SCOTT. The PL/SQL to grant this is dbms_java.grant_permission( 'SCOTT', 'SYS:java.io.FilePermission', 'c:\windows\system32\cmd.exe', 'execute' )
    I am running on Windows7 Platform & Oracle 12c 64bit version.
    Waiting for all of your expert advice.

    Yes my Oracle server is installed on Windows 7.
    Actually the user (SCOTT) was missing soome Network ACLs. I got it resolved by giving the connect, resolve network acls for the required host as below:
    BEGIN
      DBMS_NETWORK_ACL_ADMIN.CREATE_ACL(acl         => 'dbserver.xml',
                                        description => 'DB Server ACL',
                                        principal   => 'SCOTT',
                                        is_grant    => true,
                                        privilege   => 'connect');
      DBMS_NETWORK_ACL_ADMIN.ADD_PRIVILEGE(acl       => 'dbserver.xml',
                                           principal => 'SCOTT',
                                           is_grant  => true,
                                           privilege => 'resolve');
      DBMS_NETWORK_ACL_ADMIN.ASSIGN_ACL(acl  => 'dbserver.xml',
                                        host => 'xxxxxxxxxxxx');
    END;
    COMMIT;

  • O/s commands from PL/SQL

    hi all..
    i want to execute operating system commands from PL/SQL..
    particulary imp command....
    i have used the f/wg approach but while i execute the imp command it does not responds me any output..
    I have to kill the process from o/s.
    prodn is my user
    --AS SYS user
    begin
    dbms_java.grant_permission( 'PRODN',
    'SYS:java.io.FilePermission',
    '/oracle/product/9.2.0/bin/imp',
    'execute' );
    dbms_java.grant_permission
    ('PRODN',
    'java.lang.RuntimePermission',
    'writeFileDescriptor' );
    end;
    ---------------------------------AS PRODN user
    create or replace and compile
    java source named "Util"
    as
    import java.io.*;
    import java.lang.*;
    public class Util extends Object
    public static int RunThis(String args)
    Runtime rt = Runtime.getRuntime();
    int rc = -1;
    try
    Process p = rt.exec(args);
    int bufSize = 4096;
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream(),
    bufSize);
    int len;
    byte buffer[] = new byte[bufSize];
    // Echo back what the program spit out
    while ((len = bis.read(buffer, 0, bufSize)) != -1)
    System.out.write(buffer, 0, len);
    rc = p.waitFor();
    catch (Exception e)
    e.printStackTrace();
    rc = -1;
    finally
    return rc;
    create or replace
    function RUN_CMD(p_cmd in varchar2) return number
    as
    language java
    name 'Util.RunThis(java.lang.String) return integer';
    create or replace procedure RC(p_cmd in varchar2)
    as
    x number;
    begin
    x := run_cmd(p_cmd);
    end;
    i executed the command as follows in sql>
    BEGIN
    rc('/oracle/product/9.2.0/bin/imp tim/tim file=/home/oracle/vas4359.dmp log
    =log1.log');
    END;
    it runs fine creates log1.log in / home/oracle but there is no output in
    log1.log file.
    I have to kill the process as follows after which sql> displays me :
    "PL/SQL procedure successfully completed."
    [oracle@msebdb oracle]$ ps aux|grep imp
    oracle 20052 0.0 0.2 12732 4328 ? S 11:57 0:00 /oracle/product/
    .2.0/bin/imp file=/home/oracle/vas4359.dmp log=log1.log
    oracle 20102 0.0 0.0 3692 652 pts/2 S 12:03 0:00 grep imp
    Note:There is no tim user.. i just want to get error in my log file..
    my further steps are to read this log file and send email/notifications to users.
    my main program is as f/ws:
    /*imp and check log file*/
    create or replace procedure abc
    as
    v_exists boolean;
    v_exists_log boolean;
    ex_open BOOLEAN;
    flen NUMBER;
    bsize NUMBER;
    vInHandle_open utl_file.file_type;
    vInHandle_open_log utl_file.file_type;
    v_filelog utl_file.file_type;
    v_fl utl_file.file_type;
    v_pth varchar2(60);
    v_fnm varchar2(60);
    v_file_exists varchar2(60);
    v_logline varchar2(30) ;
    v_search varchar2(30) :='ORA-';
    vNewLine VARCHAR2(32767);
    cursor c_fname is
    select full_pth ,f_name from upld_file_t where f_name in
    (select f_name from tbl_file_nm where typ_file='D' AND DEL_FLG='N');
    BEGIN
    for v_sec in c_fname
    loop
    utl_file.fgetattr(LOCATION=>'ORALOAD',
    FILENAME=>v_sec.f_name,
    FEXISTS=>v_exists,
    FILE_LENGTH=> flen,
    BLOCK_SIZE=>bsize);
    if v_exists
    THEN
    dbms_output.put_line('File Exists' || v_sec.f_name);
    v_file_exists:=v_sec.f_name;
    --dbms_output.put_line('v_file_exists ' || v_file_exists);
    dbms_output.put_line('File Length: ' || TO_CHAR(flen));
    dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
    dbms_output.put_line(' before file open');
    vInHandle_open := utl_file.fopen('ORALOAD',v_sec.f_name,'R',32767);
    -- dbms_output.put_line(' after file open');
    -- do whater with teh file data
    v_fl:=utl_file.fopen('ORALOAD','v_sec.f_name','W');
    dbms_output.put_line('v_sec.f_name');
    IF utl_file.is_open(vInHandle_open)
    THEN
    /*if log file exists..*/
    utl_file.fgetattr(LOCATION=>'ORALOAD',
    FILENAME=>v_sec.f_name||'.log',
    FEXISTS=>v_exists_log,
    FILE_LENGTH=> flen,
    BLOCK_SIZE=>bsize);
    if v_exists_log
    then
    dbms_output.put_line('file' ||v_sec.f_name||'.log');
    v_filelog :=utl_file.fopen('ORALOAD',v_sec.f_name||'.log','W',32767);
    LOOP
    BEGIN
    UTL_FILE.PUT(v_filelog,'ORA-');
    exit;
    END;
    END LOOP;
    else
    dbms_output.put_line('File not found' || v_sec.f_name);
    END IF;
    UTL_FILE.fclose(v_filelog);
    dbms_output.put_line('Closed File ' || v_sec.f_name|| '.log');
    END IF;
    /*imp THE FILE and read log file*/
    utl_file.fgetattr(LOCATION=>'ORALOAD',
    FILENAME=>v_sec.f_name||'.log',
    FEXISTS=>v_exists_log,
    FILE_LENGTH=> flen,
    BLOCK_SIZE=>bsize);
    if v_exists_log
    then
    dbms_output.put_line('file' ||v_sec.f_name||'.log');
    v_filelog :=utl_file.fopen('ORALOAD',v_sec.f_name||'.log','R',32767);
    LOOP
    BEGIN
    UTL_FILE.get_line(v_filelog,v_logline);
    if v_logline = v_search then
    dbms_output.put_line(v_logline);
    end if;
    UTL_FILE.fclose(v_filelog);
    dbms_output.put_line('Closed File ' || v_sec.f_name|| '.log');
    exit;
    END;
    END LOOP;
    else
    dbms_output.put_line('File not found' || v_sec.f_name);
    END IF;
    END IF;
    END LOOP;
    --close the curosr .. check this
    EXCEPTION
    WHEN UTL_FILE.ACCESS_DENIED THEN
    DBMS_OUTPUT.PUT_LINE('No Access!!!');
    when others then
    dbms_output.put_line('ERROR (open_file) => '||sqlcode);
    dbms_output.put_line('MSG (open_file) => '||sqlerrm);
    end;

    Hi,
    You might have better luck in the PL/SQL or Java forums. They are located here:
    PL/SQL: PL/SQL
    Java: http://forums.oracle.com/forums/forum.jsp?forum=99
    Good luck,
    Mark

  • Using 10g SQL Plus from a batch file

    I installed the Oracle Database 10g Express Client and configured my ODBC and tnsnames.ora settings, and I think I have it all right, because when I open the SQL Command Line I can connect to the database just fine using:
    connect <username>/<password>@<database>
    However, when I try to log in through a non-SQL command line or batch file using the syntax:
    sqlplus <username>/<password>@<database>
    I get the error "ORA-12154: TNS:could not resolve the connect identifier specified." That syntax used to work for me with 9.2. Does the functionality still exist in 10.2?
    Update #1:
    The sqlplus login works from a normal command widow if I first CD C:\XEClient\bin. I still can't get it to work from a batch file, even when I specify the path. Does this mean the batch file command can't find my tnsnames.ora? If so, what environment variable do I need to set? I already have the client directory in the Path and ORACLE_HOME variables.
    Update #2:
    I started randomly trying every Oracle environment variable I could find a reference to, and found that setting TNS_ADMIN to the C:\XEClient\bin directory worked.
    I don't need any responses to this post, so it can be deleted by an admin.
    Edited by: user11241849 on Jan 13, 2010 12:05 PM

    Hi Toni,
    It is my first post here and I though telepathy already operated here :)
    it is Oracle 9 I am talking about.
    actually, I have 2 batch files
    File 1, lets say the file name is batch1.bat and the contents are
    sqlldr user/pwd@tnsname control='Control.ctl'  data='data.txt'
    File 2, batch2.bat and the contents are
    Batch1.bat
    Sqlplus user/pwd@tnsname @callproc.sql
    Now if I run Batch2.bat, then only the first line to be executed which calling the batch1.bat , the second line (sqlplus) never executed!
    Best Regards,
    Yousef
    Edited by: yousef_bayyari on 10-Nov-2009 06:25

  • How to load SQL scripts from a text file.

    Hi, i tried several time to load a text file/SQL script with 10 different tables and data, but 10g Express doesen't allows me to do that, any one can direct me or point out to me what i should do or do i need to adopt any special method to to get this done. i am sure there must be some thing where you can upload SQL scripts from a text file (in SQL command editor!). thanks

    Hi,
    see my other answer here:
    SQL command editor doesn't take more than 1 insert command
    This seems to be a duplicate question, right? Or am I missing something?
    Regards,
    ~Dietmar.

  • Running sql script from pl/sql

    Is there any standard way to run an external sql script from pl/sql
    I really appreciate any assistance.

    If you want, I did start writing a function reading and executing statements out of sql script with utl_file.
    can I issue this command in PL/SQL: EXECUTE IMMEDIATE '@filename.sql';
    the function could be extended for DDL, session setting, etc...
    Regards
    Laurent

  • Calling Operating System Commands from PL/SQL using java

    Calling Operating System Commands from PL/SQL - The Java Way
    wlth help of given thread link,
    Calling OS Commands from Plsql
    but i had user privilege problem
    Declare
    x Varchar2(2000);
    Begin
    x := OSCommand_Run('/tmp/sri/GROUP_ho.sh');
    DBMS_OUTPUT.Put_Line(x);
    End;
    o/p;
    can't exec: /tmp/sri/GROUP_ho.sh lacks user privilege
    i done the grant privillage also
    part
    dbms_java.grant_permission('abcd', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
    dbms_java.grant_permission('abcd', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
    dbms_java.grant_permission('
    abcd', 'SYS:java.io.FilePermission','<<ALL FILES>>', 'execute');
    again
    get
    can't exec: /tmp/sri/GROUP_ho.sh lacks user privilege
    same error;

    Process management at the OS level should prevent execution continuing in the calling code until the command has completed (either successfully or with error).
    If the low level java code were to spawn child process threads then execution could continue, but I'm guessing the Java function your talking about doesn't do that and just calls the operating system to execute the command and waits for the returning code to come back.

  • How to: execute operating system commands from PL/SQL

    System: IBM pSeries AIX
    Oracle: 11g Enterprise
    Is there a quick way to execute operating system commands from PL/SQL?
    Without creating a JAVA class....
    I need to execute a Loader script.
    I want to do this from within PL/SQL.
    I need to to do 4 things.
    Three require SQL scripts.
    The fourth is SQL*Loader.
    The usual way is with a UNIX script calling 3 SQL scripts and one Loader control file.
    Is there way to do all this from within ONE PL/SQL SCRIPT ??
    Just let me know.

    Is there a quick way to execute operating system commands from PL/SQL?
    NO
    Without creating a JAVA class....
    I need to execute a Loader script.
    I want to do this from within PL/SQL.
    WHY ON EARTH, such action will create a new connection and become a resource hog?
    I need to to do 4 things.
    Three require SQL scripts.
    The fourth is SQL*Loader.
    The usual way is with a UNIX script calling 3 SQL scripts and one Loader control file.
    Which is perfect for the purpose.
    Is there way to do all this from within ONE PL/SQL SCRIPT ??
    Just let me know.
    NO, there is NOT. Nor should there be. Why create unstable ineffcient Mickey Mouse systems, glued together by hacking?
    Sybrand Bakker
    Senior Oracle DBA

  • How to use lengthy query in a "SQL Command from Variable"

    My oracle SQL Query length is more than 14000 characters, so how can i use this for "SQL command from variable".
    Sarvan

    Either create a view or a table valued UDF. Using stored procedure in OLEDB source has some gotchas it will not pick up metadata information by default due to late binding.
    http://consultingblogs.emc.com/jamiethomson/archive/2006/12/20/SSIS_3A00_-Using-stored-procedures-inside-an-OLE-DB-Source-component.aspx
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • Run multiple reports from PL/SQL

    I am planning to run reports from a PL/SQL batch program. My requirement is i need to generate multiple(around 50) reports with only a difference in one numeric parameter which will go from 1 to 50. I am planning to invoke the report inside a loop which goes from 1..50. I heard that i need to use a utl_http package which i havent used till now. Does somone has a sample code for running a report from pl/sql. i need it ASAP. Thanks in advance

    hi rejesh,
    we have an pl/sql API in reports to do exactly what you want to do.
    The Reports Event Driven Publishing API
    find the documentation including examples in the reports getting
    started on technet.
    http://otn.oracle.com/products/reports/htdocs/getstart/docs/a92102_01.pdf
    Chapter 11
    regards,
    christian

Maybe you are looking for