Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute scripts by relative path in Oracle SQL Developer

First, this question relates to Oracle SQL Developer 3.2, not SQL*Plus or iSQL, etc. I've done a bunch of searching but haven't found a straight answer.

I have several collections of scripts that I'm trying to automate (and btw, my SQL experience is pretty basic and mostly MS-based). The trouble I'm having is executing them by a relative path. for example, assume this setup:

scripts/A/runAll.sql
       | /A1.sql
       | /A2.sql
       |
       /B/runAll.sql
         /B1.sql
         /B2.sql

I would like to have a file scripts/runEverything.sql something like this:

@@/A/runAll.sql
@@/B/runAll.sql

scripts/A/runAll.sql:

@@/A1.sql
@@/A2.sql

where "@@", I gather, means relative path in SQL*Plus.

I've fooled around with making variables but without much luck. I have been able to do something similar using '&1' and passing in the root directory. I.e.: scripts/runEverything.sql:

@'&1/A/runAll.sql' '&1/A'
@'&1/B/runAll.sql' '&1/B'

and call it by executing this:

@'c:/.../scripts/runEverything.sql' 'c:/.../scripts'

But the problem here has been that B/runAll.sql gets called with the path: c:/.../scripts/A/B.

So, is it possible with SQL Developer to make nested calls, and how?

like image 522
Didjit Avatar asked Jun 02 '14 20:06

Didjit


3 Answers

This approach has two components:

-Set-up the active SQL Developer worksheet's folder as the default directory.

-Open a driver script, e.g. runAll.sql, (which then changes the default directory to active directory), and use relative paths within the runAll.sql script to call sibling scripts.

  1. Set-up your scripts default folder. On the SQL Developer toolbar, Use this navigation:

    Tools > Preferences

    In the preference dialog box, navigate to Database > Worksheet > Select default path to look for scripts.

    Enter the default path to look for scripts as the active working directory:

    "${file.dir}"

  2. Create a script file and place all scripts associated in it:

    runAll.sql

    A1.sql

    A2.sql

    The content of runAll.sql would include:

    @A1.sql;

    @A2.sql;

To test this approach, in SQL Developer, click on File and navigate and open the script\runAll.sql file.

Next, select all (on the worksheet), and execute.

Through the act of navigating and opening the runAll.sql worksheet, the default file folder becomes "script".

like image 142
Patrick Bacon Avatar answered Sep 27 '22 23:09

Patrick Bacon


I don't have access to SQL Developer right now so i can't experiment with the relative paths, but with the substitution variables I believe the problem you're seeing is that the positional variables (i.e. &1) are redefined by each start or @. So after your first @runAll, the parent script sees the same &1 that the last child saw, which now includes the /A.

You can avoid that by defining your own variable in the master script:

define path=&1
@'&path/A/runAll.sql' '&path/A'
@'&path/B/runAll.sql' '&path/B'

As long as runAll.sql, and anything that runs, does not also (re-define) path this should work, and you just need to choose a unique name if there is the risk of a clash.

Again I can't verify this but I'm sure I've done exactly this in the past...

like image 21
Alex Poole Avatar answered Sep 27 '22 23:09

Alex Poole


you need to provide the path of the file as String , give the patch in double quote it will work

**

For Example @"C:\Users\Arpan Saini\Zions R2\Reports Statements and Notices\Patch\08312017_Patch_16.2.3.17\DB Scripts\snsp.sql";

**

like image 29
Arpan Saini Avatar answered Sep 27 '22 21:09

Arpan Saini