Announcement

Collapse
No announcement yet.

StoredProcedure, Cursor und Update

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

  • StoredProcedure, Cursor und Update

    Hallo,
    ich habe eine StoredProcedure wie folgt
    <blockquote>
    <font face="Courier New" size="2"><b>DECLARE cr CURSOR FOR
    &nbsp;&nbsp;&nbsp; SELECT a, b, c, d
    &nbsp;&nbsp;&nbsp; FROM TabelleXYZ
    &nbsp;&nbsp;&nbsp; WHERE b > irgendetwas
    &nbsp;&nbsp;&nbsp; AND c = nochetwas
    &nbsp;&nbsp;&nbsp; ORDER BY a, d

    OPEN cr

    FETCH NEXT FROM cr INTO @a, @b, @c, @d

    WHILE (@@FETCH_STATUS = 0)
    BEGIN
    &nbsp;&nbsp;&nbsp; @bNeu = dbo.f1 (@b, @c, @d)
    &nbsp;&nbsp;&nbsp; @cNeu = dbo.f2 (@bNeu, @c, @d)
    &nbsp;&nbsp;&nbsp; @dNeu = dbo.f3 (@bNeu, @cNeu, @d)
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp; -- gibt es hierfuer eine bessere Loesung
    &nbsp;&nbsp;&nbsp; UPDATE TablelleXYZ
    &nbsp;&nbsp;&nbsp; SET b = @bNeu,
    &nbsp;&nbsp;&nbsp;&nbsp; c = @cNeu,
    &nbsp;&nbsp;&nbsp;&nbsp; d = @dNeu
    &nbsp;&nbsp;&nbsp; WHERE a = @a
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp; FETCH NEXT FROM cr INTO @a, @b, @c, @d
    END

    CLOSE cr

    DEALLOCATE cr</b></font>
    </blockquote>

    In der while-Schleife ist noch anderer Code mit dabei, den ich exemplarisch
    entfernt habe.
    Mir ist dabei wichtig eine Alternative zu finden, die den Update ersetzt.
    Kann man nicht gleich eine Wertzuweisung machen, da ich eh auf den Datensatz <b>&quot;a&quot;</b>
    stehe?

    Gruss und Dank
    &nbsp;&nbsp; Dietmar

  • #2
    Hallo,
    über <i>DECLARE CURSOR FOR UPDATE</i> in Kombination mit <i>WHERE CURRENT OF</i> steht in der Tat eine Alternative zur Verfügung. Das folgende Beispiel verdeutlich das Prinzip (hoffentlich) besser als tausend Worte:
    <code>
    <b>CREATE</b> <b>TABLE</b> CursorTestTbl
    (
    recid <b>INTEGER</b> <b>NOT</b> <b>NULL</b> <b>IDENTITY</b> <b>PRIMARY</b> <b>KEY</b>,
    wert <b>VARCHAR</b>(99) <b>NOT</b> <b>NULL</b>,
    datum DATETIME <b>NOT</b> <b>NULL</b> <b>DEFAULT</b> <b>CURRENT_TIMESTAMP</b>
    )
    <b>GO</b>
    <font color="#008080">-- Testdatensätze einfügen</font>
    <b>SET</b> NOCOUNT <b>ON</b>
    <b>DECLARE</b> @iCount <b>INTEGER</b>
    <b>DECLARE</b> @Wert <b>VARCHAR</b>(99)
    <b>SET</b> @iCount = 1
    <b>WHILE</b> @iCount &lt; 20
    <b>BEGIN</b>
    <b>SET</b> @Wert = <font color="#9933CC">'Test '</font> + <b>CAST</b>(@iCount <b>AS</b> <b>VARCHAR</b>)
    <b>INSERT</b> <b>INTO</b> CursorTestTbl (wert) <b>VALUES</b> (@Wert)
    <b>SET</b> @iCount = @iCount + 1
    <b>END</b>
    <b>GO</b>
    <font color="#008080">-- Update-Cursor aktualisiert einen Spaltenwert</font>
    <b>DECLARE</b> @recid <b>INTEGER</b>
    <b>DECLARE</b> @wert <b>VARCHAR</b>(99)
    <b>DECLARE</b> @datum DATETIME
    <b>DECLARE</b> cr <b>CURSOR</b> <b>FOR</b>
    <b>SELECT</b> recid,wert,datum <b>FROM</b> CursorTestTbl <b>ORDER</b> <b>BY</b> recid
    <b>FOR</b> <b>UPDATE</b>
    <b>OPEN</b> cr
    <b>FETCH</b> <b>NEXT</b> <b>FROM</b> cr <b>INTO</b> @recid, @wert, @datum
    <b>WHILE</b> (@@FETCH_STATUS = 0)
    <b>BEGIN</b>
    <b>UPDATE</b> CursorTestTbl
    <b>SET</b> wert = @wert + <font color="#9933CC">'CursorUpdate'</font>
    <b>WHERE</b> <b>CURRENT</b> <b>OF</b> cr;
    <b>FETCH</b> <b>NEXT</b> <b>FROM</b> cr <b>INTO</b> @recid, @wert, @datum
    <b>END</b>
    <b>CLOSE</b> cr
    <b>DEALLOCATE</b> cr
    </code&gt

    Comment

    Working...
    X