Announcement

Collapse
No announcement yet.

Rückgabewert erzeugen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rückgabewert erzeugen

    Hallo,

    ich habe unter Windows ein Batch-File erstellt, in dem ich mittels sqlplus mehrere PL/SQL-Scripts starte.
    Nun möchte ich abhängig vom Ergebnis dieser Scripte (Rückgabewert) die Ausführung der folgenden Scripte steuern sowie dies Rückgabewerte in eine Datei schreiben.
    Jetzt habe ich zwei Problem, die ich bisher nicht lösen konnte:
    1. Wie kann ich dafür sorgen, dass das PL/SQL-Script einen Rückgabewert ausgibt, den ich anschließend im Batchfile auswerten kann.
    2. wie komme ich im Batchfile an diesen Rückgabewert.

    Nebenbei würde ich noch gern wissen, ob ich nur Returncodes oder auch Strings zurück geben kann.

    Gruß

    cheapy
    Zuletzt editiert von cheaptrick_la; 25.07.2008, 15:26.

  • #2
    Wie kann ich dafür soregn, dass das PL/SQL-Script einen Rückgabewert ausgibt, den ich anschließend im Batchfile auswerten kann.
    So:
    Code:
    variable retVal number
    begin
     :retVal:=deine_plsql_function;
    end;
    /
    exit :retVal

    Dim
    Zitat Tom Kyte:
    I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

    Comment


    • #3
      Hallo Dimitri,

      irgendetwas läuft da falsch, ich bekomme die folgende Fehlermeldung:

      variable retVal number
      *
      ORA-06550: line 76, column 17:
      PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following:

      := . ( @ % ; not null range default character
      The symbol "; was inserted before "NUMBER" to continue.

      Lasse ich das Wörtchen 'variable' weg, bekomme ich die folgende Meldung:

      DECLARE
      *
      ORA-01008: not all variables bound

      Und nun?

      Gruß

      cheapy
      Zuletzt editiert von cheaptrick_la; 25.07.2008, 15:46.

      Comment


      • #4
        Das sind sqlplus Befehle. Also nicht in den PL/SQL Block reinschreiben.
        So in etwa:
        Code:
        D:\database\scripts>sqlplus phx_main_dz/****@vdce1r1
        
        SQL*Plus: Release 10.2.0.3.0 - Production on Fr Jul 25 15:36:49 2008
        
        Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.
        
        
        Verbunden mit:
        Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
        With the Partitioning, Real Application Clusters, OLAP and Data Mining options
        
        SQL> variable retval number;
        SQL> begin
          2  select 1 into :retval from dual;
          3  end;
          4  /
        
        PL/SQL-Prozedur erfolgreich abgeschlossen.
        
        SQL> print retval
        
            RETVAL
        ----------
                 1
        
        SQL> exit :retval
        Verbindung zu Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
        With the Partitioning, Real Application Clusters, OLAP and Data Mining options beendet
        Dim
        Zitat Tom Kyte:
        I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

        Comment


        • #5
          Hallo Dimitri,

          auch das funktioniert nicht. Setze ich die Deklaration der Variable vor den PL/SQL-Teil, dan bekomme ich die folgenden Meldungen:

          SQL> number;
          variable retVal number
          *
          ORA-00900: invalid SQL statement

          SQL> ALTER SESSION SET NLS_TERRITORY='GERMANY';

          und

          DECLARE
          *
          ORA-01008: not all variables bound

          Gruß

          Cheapy

          Comment


          • #6
            SQL> number;
            variable retVal number
            *
            Hmm hast du das wirklich so eingegeben? Kann ich mir nicht vorstellen.

            Welche sqlplus Version verwendest du denn? Funktioniert das Beispiel wie ich es geschrieben hab?
            Vor allem irritiert mich, dass Du Oracle Fehlermeldungen bekommst. Das ist allerdings eine rein sqlplus interne Sache und wird nicht an die DB weitergebeben.

            Dim
            Zitat Tom Kyte:
            I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

            Comment


            • #7
              Hallo Dimitri,

              nein, da steht folgendes:

              variable retVal number;
              ALTER SESSION SET NLS_TERRITORY='GERMANY';

              Ich hab das ganz zunächst mit jbsql getestet, damit teste ich alle meine SQL und PL/SQL-Scripts weil es ein besseres Ausgabeformat hat un auch so besser zu handhaben ist.

              Gruß

              cheapy

              Comment


              • #8
                Ich hab das ganz zunächst mit jbsql getestet,
                Na das ist dann die Erklärung. Damit läuft das natürlich nicht.
                Ich würd aber generell sqlplus für Kommandozeilenaufrufe verwenden, denn es ist sehr leistungsfähig, gut dokumentiert und überall verfügbar sofern ein Oracle Client installiert ist.

                Du kannst übrigends das Ausgabeformat von sqlplus beliebig formatieren.

                Dim
                Zitat Tom Kyte:
                I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

                Comment


                • #9
                  So,
                  ich hab mal kurz in der Doku nachgesehen. Lass den ; weg. Dann erkennt er es als Clientkommando.

                  http://users.pandora.be/jbosman/jbsql/index.html

                  Dim
                  Zitat Tom Kyte:
                  I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

                  Comment


                  • #10
                    Kommando zurück. Hab ich wohl auf die schnelle falsch verstanden.
                    Sieht so aus, als ob jbsql keine Bindvariablen unterstützt.

                    Dim
                    Zitat Tom Kyte:
                    I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

                    Comment


                    • #11
                      Hallo,

                      das Script mit dem ich arbeite hat knapp 400 Zeilen. Mit Drag & Drop bekomme ich aber maximal 110 Zeilen nach PL/SQL.
                      Ist nicht sehr Spaßig ein Script in mehreren Schritten kopieren zu müssen.
                      Gibt's dafür auch nen Parameter den ich umstellen kann damits nicht so mühselig bleibt?

                      CU

                      cheapy

                      Comment


                      • #12
                        Versteh ich nicht. Was willst Du kopieren?
                        Wenn Du ein Script in sqlplus starten willst, dann einfach so:
                        Code:
                        @pfad\zu\skript\skript.sql
                        Oder auch so:
                        Code:
                        sqlplus user/password@tnsname @pfad\zu\skript\skript.sql
                        Fertig.
                        Zitat Tom Kyte:
                        I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

                        Comment


                        • #13
                          OK, ich habs dann mal so aufgerufen wie ich's im Batchfile mache bzw. wie Du es beschrieben hast.
                          Jetzt gibt's zumindest kein Gemecker mehr. Was ich nun geren wissen möchte ist, wie ich nun an den Returncode des Scripts komme.

                          Comment


                          • #14
                            Hmm da bin ich mir jetzt nicht sicher. Ich schreib meine Skripte meistens unter Unix.
                            Aber kannst den Wert unter Win nicht einfach zuweisen?
                            Also SET variable=sqlplus_aufruf
                            Oder wird die ERRORLEVEL Variable damit gefüttert?
                            Wie gesagt: Auf der cmd Skriptebene kann ich dir nicht mehr wirklich weiterhelfen.

                            Dim
                            Zitat Tom Kyte:
                            I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

                            Comment


                            • #15
                              Rückgabewert

                              Hallo cheaptrick_la,

                              hier hast Du ein Beispiel

                              PL/SQL Skripte
                              Code:
                              -- PL/SQL Skript 1
                              whenever sqlerror exit 1
                              set verify off
                              set feedback off
                              set serverout on size 100000
                              exec dbms_output.enable( 100000 )
                              
                              DECLARE
                              	-- Error?
                              	error		varchar2(1) := substr(upper('&1'),1,1);
                              	ex_Error	Exception;
                              BEGIN
                              	if( error = 'Y' )	then
                              		raise ex_Error;
                              	end if;
                              END;
                              /
                              exit 0
                              
                              -- PL/SQL Skript 2
                              whenever sqlerror exit 1
                              set feedback off
                              set serverout on size 100000
                              exec dbms_output.enable( 100000 )
                              
                              BEGIN
                              	dbms_output.put_line('Skript 2');
                              END;
                              /
                              exit 0
                              
                              -- PL/SQL Skript 3
                              whenever sqlerror exit 1
                              set feedback off
                              set serverout on size 100000
                              exec dbms_output.enable( 100000 )
                              
                              BEGIN
                              	dbms_output.put_line('Skript 3');
                              END;
                              /
                              exit 0
                              Batch-Datei
                              Code:
                              @echo off
                              REM * sinnlos.cmd -> PL/SQL Fehler provozieren
                              REM * Werte in spitzen Klammern <> ersetzen
                              
                              
                              REM -s: silent
                              sqlplus -s <user>/<password>@<db> @sql_1 "Y"
                              if errorlevel 1 (
                                  echo Fehler: Skript 3 wird ausgeführt
                              	goto :SKRIPT_3
                              )
                              
                              echo kein Fehler: Skript 2 wird ausgeführt
                              sqlplus -s <user>/<password>@<db> @sql_2
                              goto :END
                              
                              :SKRIPT_3
                              sqlplus -s <user>/<password>@<db> @sql_3
                              
                              :END
                              echo Besser Unix verwenden...
                              pause > nul
                              Test
                              Code:
                              D:\NONBKU\SQL>sinnlos
                              DECLARE
                              *
                              FEHLER in Zeile 1:
                              ORA-06510: PL/SQL: Unbehandelte benutzerdefinierte Exception
                              ORA-06512: in Zeile 7
                              
                              
                              Fehler: Skript 3 wird ausgef³hrt
                              Skript 3
                              Besser Unix verwenden...
                              
                              D:\NONBKU\SQL>
                              Gruß, Siegfried

                              Comment

                              Working...
                              X