How to implements "select tmp.tagid from (exec store_procedure) tmpA"

Posted on 2005-04-18
Last Modified: 2008-03-17
I have a procedure A which is something 'Select * from TBL A'. But procedure is very long actraully.

Another procedure will select single field from result of the procedure A.
I can repeat that exactly part in procedure A,so like procedure B become
select tmpA.tagid from (Select * from TBL A) tmpA
but I dont want to repeat the code of procedure A in Procedure B.

Is there something like--
Select tmpA.tagid from (exec procedureA) tmpA

thank you every much
Question by:yangbin991
    LVL 9

    Expert Comment

    There is - use a user defined function. I'm not much good at them so please look in books online, or wait for somebody to tell you exactly how to do it.

    LVL 7

    Accepted Solution

    One way that I've accomplished this is to create a temporary table to hold the results of the proceure A
    >>  CREATE TABLE #tmpA (Field1 int, Field2 int, ...)

    Then you insert the data from the procedure into the temporary table:
    >>  INSERT INTO #tmpA
    >>  EXEC procedureA

    Then finally select the fields you want

    >>  SELECT Field1 FROM #tmpA

    You can encapsulate this into another procedure, that way if you change the way the procedure A operates you don't have to maintain code in 2 places.

    Hope this helps. :)

    Author Comment

    thank tloie.

    create tmp table actrually is not good. because it should be slow down the process. But seems only this way to solve.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Read about achieving the basic levels of HRIS security in the workplace.
    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

    8 Experts available now in Live!

    Get 1:1 Help Now