Announcement

Collapse
No announcement yet.

Stored Procedures

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

  • Stored Procedures

    Guten Morgen!<br>
    Ist es möglich, wie unter IB, StoredProcedures in Select-Statements auszuführen?<br>
    z.B. Select * from SP...<br>
    <br>
    Gruß,<br>
    Christian

  • #2
    Hallo,

    für diesen Zweck gibt es beim MS SQL Server die <b>Functions</b>. Das folgende Beispiel aus der Hilfedatei des MS SQL Server 2000 (Hilfeseite <i>CREATE FUNCTION</i>) verdeutlicht dies:
    <pre>
    CREATE FUNCTION fn_FindReports (@InEmpId nchar(5))
    RETURNS @retFindReports TABLE (empid nchar(5) primary key,
    empname nvarchar(50) NOT NULL,
    mgrid nchar(5),
    title nvarchar(30))
    /*Returns a result set that lists all the employees who report to given
    employee directly or indirectly.*/
    AS
    BEGIN
    DECLARE @RowsAdded int
    -- table variable to hold accumulated results
    DECLARE @reports TABLE (empid nchar(5) primary key,
    empname nvarchar(50) NOT NULL,
    mgrid nchar(5),
    title nvarchar(30),
    processed tinyint default 0)
    -- initialize @Reports with direct reports of the given employee
    INSERT @reports
    SELECT empid, empname, mgrid, title, 0
    FROM employees
    WHERE empid = @InEmpId
    SET @RowsAdded = @@rowcount
    -- While new employees were added in the previous iteration
    WHILE @RowsAdded > 0
    BEGIN
    /*Mark all employee records whose direct reports are going to be
    found in this iteration with processed=1.*/
    UPDATE @reports
    SET processed = 1
    WHERE processed = 0
    -- Insert employees who report to employees marked 1.
    INSERT @reports
    SELECT e.empid, e.empname, e.mgrid, e.title, 0
    FROM employees e, @reports r
    WHERE e.mgrid=r.empid and e.mgrid <> e.empid and r.processed = 1
    SET @RowsAdded = @@rowcount
    /*Mark all employee records whose direct reports have been found
    in this iteration.*/
    UPDATE @reports
    SET processed = 2
    WHERE processed = 1
    END

    -- copy to the result of the function the required columns
    INSERT @retFindReports
    SELECT empid, empname, mgrid, title
    FROM @reports
    RETURN
    END
    GO

    -- Example invocation
    SELECT *
    FROM fn_FindReports('11234')
    GO
    </pre&gt

    Comment


    • #3
      Hallo Andreas,<br>
      danke für Deine Antwort. Auf Functions bin ich deshalb nicht gekommen, da es im IB auch nicht so ohne weiteres damit funzt. Aber Ok, es funktioniert super.<br>
      <br>
      Gruß,<br>
      Christia

      Comment


      • #4
        Hallo,<br>
        hab jetzt doch noch nen Problem:<br>
        select name from personen where id = (select * from func_aktueller_nutzer(personen.id, '11.12.2003'))<br>
        Fehlermeldung: Falsche Syntax in der nähe von '.'.<br>
        Ersetze ich das "personen.id" durch einen festen wert, dann funktioniert das.<br>
        Woran kann das liegen?<br>
        PS.: das SQL-Statement ist nicht wirklich sinnig, ich weiß, dient auch nur als Beispiel.<br>
        <br>
        Gruß,<br>
        Christia

        Comment


        • #5
          N'Abend,

          bei Funktionen die Tabellen zurückgehen sind nur konstanten Parameter erlaubt.
          Die Übergabe von Datenbankobjekten wie Spalten, Tabellen ist also nicht möglich.

          Ral

          Comment


          • #6
            Hallo,<br>
            gibt es da dann eine andere Möglichkeit?<br>
            <br>
            Gruß,<br>
            Christia

            Comment


            • #7
              Mahlzeit,

              andere Möglichkeit für was ?
              Wenn du dein obiges Beispiel meinst dann nimm die Personen Tabelle direkt mit in die Stored function auf und übergib nur noch das Datum an diese.

              Ral

              Comment

              Working...
              X