Can stored procedure be called from sql query

  • Hi Experts,

    1)Is it possible to call a Stored procedure from a SQL query?

    2)Is it possible to combine the result of two Stored Procedures that returns result of select statements ?

    If so can you give me some examples..

    Regards,

    Eswin

    Tanx ๐Ÿ˜€

  • To execute a stored procedure, use the syntax below:

    exec storedprocedurename parameterlist

    For example, if my stored procedure name was usp_GetSalesByState with a parameter of StateID, I would use:

    exec usp_GetSalesByState 'GA'

    OR

    execute usp_GetSalesByState 'GA'

    As for joining the results of 2 stored procedures, you may need to clarify. You can return more than 1 result set from a stored procedure. You may also use a union/union all operation within a stored procedure to join 2 data sets. You can also write the results of 2 stored procedures to temporary tables and then union those tables -- all within a 3rd stored procedure.

    I hope that this helps. Thanks.

    Chris

  • Eswin (11/16/2010)


    1)Is it possible to call a Stored procedure from a SQL query

    Kind of:

    ...INSERT INTO...EXEC...

    The target table columns must match the output of the stored procedure - sequence and datatype.

    Eswin (11/16/2010)


    2)Is it possible to combine the result of two Stored Procedures that returns result of select statements ?

    INSERT INTO #Table1 EXEC Sproc1

    INSERT INTO #Table2 EXEC Sproc2

    SELECT t1.*

    FROM #Table1 t1

    LEFT JOIN #Table2 t2

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • You can use user defined functions that returns tables and then treat the tables returned from the function as any other table

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply