20

* EDIT6: * This is what ended up working for me (from accepted answer):

var ret1 number
var tran_cnt number
var msg_cnt number
var rc refcursor
exec :tran_cnt := 0
exec :msg_cnt := 123
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => :rc)
print :tran_cnt
print :msg_cnt
print :rc

The SQL Developer makes this super-hard / impossible?. I do not care if the utility is command-line-based; I just want to be able to run and view it quickly. It would be nice if it captured errors well as well. it would be nice to be able to log in gradually(interactively), as well as specifying everything at once (similar to how a typical ftp / sftp cmd-based client works).

My platform is Windows Server 2008 + Cygwin.

EDIT: Perhaps you would know how to script this using Python?

EDIT 2: In MSFT SQL server I can simply type this:

get_user 1;

then highlight it and hit F5, and I get:

login   name    
NULL    Somename

printed to the output window. The Oracle SQL developer does not help with this at all. I am not sure how to pass in a 1, I am not sure how to see the actual rows/records that come back.

EDIT3: When I type just var rc refcursor; and select it and run it, I get this error (GUI):

An error was encountered performing the requested operation:

ORA-00900: invalid SQL statement
00900.00000 - "invalid SQL statement"
* Cause:
* Action:
Vendor code 900Error at Line: 2

EDIT4:

I am trying to run a procedure the definition of which starts like this:

create or replace procedure get_account
(
    Vret_val out number,
    Vtran_count in out number,
    Vmessage_count in out number,
    Vaccount_id     IN NUMBER
    , rc1 in out sys_refcursor
)as
begin
...

I get an error:

Error starting at line 2 in command:
exec :rc := get_account(1) 
Error report:
ORA-06550: line 1, column 24:
PLS-00306: wrong number or types of arguments in call to 'GET_ACCOUNT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
rc
------

I am so close ... please help.

* EDIT 5: *

Script that I am running (functionally the same), the error was always the same:

var ret1 number
var tran_cnt number
var msg_cnt number
var rc refcursor
exec :tran_cnt := 0
exec :msg_cnt := 123
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => rc)

Script Output (on F5) (Could be several messages from several runs.):

Error report:
ORA-06550: line 1, column 134:
PLS-00201: identifier 'RC' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
anonymous block completed
anonymous block completed

Error starting at line 7 in command:
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => rc)
Error report:
ORA-06550: line 1, column 134:
PLS-00201: identifier 'RC' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
anonymous block completed
anonymous block completed

Error starting at line 7 in command:
exec get_account(Vret_val => :ret1, Vtran_count => :tran_cnt, Vmessage_count => :msg_cnt, Vaccount_id => 1, rc1 => rc)
Error report:
ORA-06550: line 1, column 134:
PLS-00201: identifier 'RC' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

Why does it say line 1, column 134? No line stretched that far ...

APC
  • 144,005
  • 19
  • 170
  • 281
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
  • 1
    What's so hard about running procedures in SQLDeveloper? You find the procedure, right-click it, and choose Run (or press Ctrl-F11), then specify the parameters in the PL/SQL block it generates. – Powerlord Jun 28 '10 at 18:40
  • 1
    Ok, I also want to see the output of the stored proc, thanks. – Hamish Grubijan Jun 28 '10 at 18:42
  • 2
    Why not frame this as a question asking how to use SQL Developer? – APC Jun 28 '10 at 19:37
  • @APC, because I have no faith in it. Please do prove me wrong. – Hamish Grubijan Jun 28 '10 at 21:00
  • 1
    I have retitled your question because I think we have got to the point where the combative tone is no longer required. I have addressed your fourth edit – APC Jul 07 '10 at 16:28
  • @Powerlord: in my current Oracle SQL dev I can do this right click thing to have a generated SQL block. BUT 1) it is not reflecting updates done to a proc (still giving old sql block even when parameters are changed and proc recompiled) and 2) generated code. is not working when there is OUT parameters (Bind Variable "RESULTTXT" is NOT DECLARED) And compared to SQL Server, it is really complicated... – Guillaume Mar 22 '12 at 14:51

5 Answers5

36

Not only is there a way to do this, there is more than one way to do this (which I concede is not very Pythonic, but then SQL*Developer is written in Java ).

I have a procedure with this signature: get_maxsal_by_dept( dno number, maxsal out number).

I highlight it in the SQL*Developer Object Navigator, invoke the right-click menu and chose Run. (I could use ctrl+F11.) This spawns a pop-up window with a test harness. (Note: If the stored procedure lives in a package, you'll need to right-click the package, not the icon below the package containing the procedure's name; you will then select the sproc from the package's "Target" list when the test harness appears.) In this example, the test harness will display the following:

DECLARE
  DNO NUMBER;
  MAXSAL NUMBER;
BEGIN
  DNO := NULL;

  GET_MAXSAL_BY_DEPT(
    DNO => DNO,
    MAXSAL => MAXSAL
  );
  DBMS_OUTPUT.PUT_LINE('MAXSAL = ' || MAXSAL);
END;

I set the variable DNO to 50 and press okay. In the Running - Log pane (bottom right-hand corner unless you've closed/moved/hidden it) I can see the following output:

Connecting to the database apc.
MAXSAL = 4500
Process exited.
Disconnecting from the database apc. 

To be fair the runner is less friendly for functions which return a Ref Cursor, like this one: get_emps_by_dept (dno number) return sys_refcursor.

DECLARE
  DNO NUMBER;
  v_Return sys_refcursor;
BEGIN
  DNO := 50;

  v_Return := GET_EMPS_BY_DEPT(
    DNO => DNO
  );
  -- Modify the code to output the variable
  -- DBMS_OUTPUT.PUT_LINE('v_Return = ' || v_Return);
END;

However, at least it offers the chance to save any changes to file, so we can retain our investment in tweaking the harness...

DECLARE
  DNO NUMBER;
  v_Return sys_refcursor;
  v_rec emp%rowtype;
BEGIN
  DNO := 50;

  v_Return := GET_EMPS_BY_DEPT(
    DNO => DNO
  );

  loop
    fetch v_Return into v_rec;
    exit when v_Return%notfound;
    DBMS_OUTPUT.PUT_LINE('name = ' || v_rec.ename);
  end loop;
END;

The output from the same location:

Connecting to the database apc.
name = TRICHLER
name = VERREYNNE
name = FEUERSTEIN
name = PODER
Process exited.
Disconnecting from the database apc. 

Alternatively we can use the old SQLPLus commands in the SQLDeveloper worksheet:

var rc refcursor 
exec :rc := get_emps_by_dept(30) 
print rc

In that case the output appears in Script Output pane (default location is the tab to the right of the Results tab).

The very earliest versions of the IDE did not support much in the way of SQL*Plus. However, all of the above commands have been supported since 1.2.1. Refer to the matrix in the online documentation for more info.


"When I type just var rc refcursor; and select it and run it, I get this error (GUI):"

There is a feature - or a bug - in the way the worksheet interprets SQLPlus commands. It presumes SQLPlus commands are part of a script. So, if we enter a line of SQL*Plus, say var rc refcursor and click Execute Statement (or F9 ) the worksheet hurls ORA-900 because that is not an executable statement i.e. it's not SQL . What we need to do is click Run Script (or F5 ), even for a single line of SQL*Plus.


"I am so close ... please help."

You program is a procedure with a signature of five mandatory parameters. You are getting an error because you are calling it as a function, and with just the one parameter:

exec :rc := get_account(1)

What you need is something like the following. I have used the named notation for clarity.

var ret1 number
var tran_cnt number
var msg_cnt number
var rc refcursor

exec :tran_cnt := 0
exec :msg_cnt := 123

exec get_account (Vret_val => :ret1, 
                  Vtran_count => :tran_cnt, 
                  Vmessage_count => :msg_cnt, 
                  Vaccount_id   => 1,
                  rc1 => :rc )

print tran_count 
print rc

That is, you need a variable for each OUT or IN OUT parameter. IN parameters can be passed as literals. The first two EXEC statements assign values to a couple of the IN OUT parameters. The third EXEC calls the procedure. Procedures don't return a value (unlike functions) so we don't use an assignment syntax. Lastly this script displays the value of a couple of the variables mapped to OUT parameters.

ruffin
  • 16,507
  • 9
  • 88
  • 138
APC
  • 144,005
  • 19
  • 170
  • 281
  • I am not sure if I have SQL*Plus feature installed. How would I check if I have it? – Hamish Grubijan Jun 29 '10 at 14:33
  • 1
    @HamishGrubijan - the simplest way of checking would be to fire up a command line window and type `sqlplus` – APC Jun 29 '10 at 15:17
  • So ... when I type sqlplus on command line (cmd.exe), it asks me for a password - it is there. However, when I type this command by itself: `var rc refcursor;` it complains about it. I guess this is because I am not in SQL*Plus Developer Worksheet ... so, how do I get there? – Hamish Grubijan Jun 29 '10 at 16:48
  • @HamishGrubijan - Which version of SQL*Dev are you using? VAR was not supported prior to 1.2.1 – APC Jun 29 '10 at 16:55
  • Version 1.5.5 Build MAIN-5969 | CVS Version Internal to Oracle SQL Developer (client-only) | Java(TM) Platform 1.5.0_06 | Oracle IDE 1.5.5.59.69 | Versioning Support 1.5.5.59.69. I could do more updates, but this is > 1.2.1, so it should be recent enough. – Hamish Grubijan Jun 29 '10 at 19:01
  • @HamishGrubijan - should be recent enough indeed. So, when you say "it complains about it" what complains? And how does it do so? Remote diagnosis is tough at the best times. So the more precise details the better. – APC Jun 29 '10 at 20:34
  • 1
    @HamishGrubjian - I have edited my response with what I think may be the solution to your problem. – APC Jul 06 '10 at 16:28
  • +150 points. Please address my final edit for the additional 25 (I think) pts bonus. – Hamish Grubijan Jul 07 '10 at 16:14
  • I have problems running this with both F5 and F9. Which one should I use? What did you mean by positional notation? Should I be removing spaces and new lines? – Hamish Grubijan Jul 07 '10 at 19:02
  • @HamishGrubijan - F5 to run the whole script, F9 to execute a single statement. So what problems are you having? Alas, telepathy and remote viewing are not amongst my skills. – APC Jul 07 '10 at 19:24
  • Thanks for the patience; I did add yet another edit at the bottom of the question. – Hamish Grubijan Jul 07 '10 at 21:04
  • @HamishGrubijan - that error is due to a typo in my posted example. References to SQL*Plus variables need a colon in front of them. So it should have been `:rc` not `rc`. Apologies. – APC Jul 08 '10 at 02:28
  • Booya! I wish ORA error messages were more helpful. Feel free to add `print :rc`, `print :tran_count` to the answer to make it more helpful to those who find it later. I'll add it as well (to my question). – Hamish Grubijan Jul 08 '10 at 14:20
  • @HamishGrubijan - if it's any consolation you are far from being the first to be bamboozled by Oracle's error messages. Some of them are very oblique indeed. Anyway, at least we goot there in the end! – APC Jul 08 '10 at 14:44
4

I am not sure how to see the actual rows/records that come back.

Stored procedures do not return records. They may have a cursor as an output parameter, which is a pointer to a select statement. But it requires additional action to actually bring back rows from that cursor.

In SQL Developer, you can execute a procedure that returns a ref cursor as follows

var rc refcursor
exec proc_name(:rc)

After that, if you execute the following, it will show the results from the cursor:

print rc
Gary Myers
  • 34,963
  • 3
  • 49
  • 74
3

My recommendation is TORA

Sujee
  • 4,985
  • 6
  • 31
  • 37
2

Have you heard of "SQuirreL SQL Client"?

http://squirrel-sql.sourceforge.net/

redcayuga
  • 1,241
  • 6
  • 4
0

There are two possibilities, both from Quest Software, TOAD & SQL Navigator:

Here is the TOAD Freeware download: http://www.toadworld.com/Downloads/FreewareandTrials/ToadforOracleFreeware/tabid/558/Default.aspx

And the SQL Navigator (trial version): http://www.quest.com/sql-navigator/software-downloads.aspx

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171