• rajemessage 14195 (11/30/2016)


    hi,

    1) should i remove loop with follwiong query.

    2) and is there any other way to do it, with out using loop.

    declare @STR varchar(max)=''

    select @STR=@str+'exec [dbo].[usptest]' + ' '''+abc.name+'''; '

    from abc

    exec(@str )

    yours sincerly

    So, in other words, you want to execute the stored procedure for every row in table ABC.

    In that case, the way you've done it has no performance benefit over the use of a loop whatsoever. The disadvantage of the method you used may be in the lack of error handling and reporting for each execution of the stored procedure and the ability to continue for other rows should an exception take place.

    The real problem here is the fact that you have a RBAR stored procedure (usptest) that will only process one row at a time. In other words, you're using a stored procedure like a scalar function. A lot of people do this because they're written a stored procedure to service a singleton request from the front end and then try to use that same stored procedure to process batches of data and it never works out for performance or resource usage.

    Based on the name of the stored procedure, I believe that the best thing to do would be to rewrite the stored procedure as an iTVF (Inline Table Valued Function) and CROSS APPLY it with the SELECT from the abc table. As you write the code for the iTVF, remember that if the word BEGIN appears in the code, then you haven't written an iTVF and performance will suffer.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)