Looping through a SQL Server Table in UDF

Posted on 2006-05-12
Last Modified: 2012-08-13
I am writing a function (SQL Server 2000 UDF) that will go through one record at at a time then perform a procedure, then add the reult to a variable then go to next record. How do I advanve the function through the table records. This is urgent!!! Thanks,
Question by:tcknudson
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    > one record at at a time then perform a procedure

    you can't call a procedure otherthab extended procedure in function
    Better you write an sp

    Author Comment

    In the sp, what is the syntax to move forward one record at a time? Do I try a loop?
    LVL 75

    Accepted Solution

    You can make use of a cursor
    for example

    create table #tab ( result int)  -- this will store the result of temp table
    declare @procArgument int

    declare cur cursor  for
    select urRecord from urTable

    OPEN cur
    FETCH NEXT FROM cur INTO @procArgument
        INSERT INTO #tab
        EXEC urStoredProcedure @procArgument  -- call ur sp

        FETCH NEXT FROM cur INTO @procArgument
    CLOSE cur

    SELECT SUM(result) Result
    FROM #Tab

    DROP TABLE #tab

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now