How do I suppress the results from a stored procedure from within a stored procedure?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-28 06:20:48

问题


I've got a stored procedure (we'll call it A) that calls another stored procedure (we'll call this one B). B includes a SELECT that I do not want to send back to the caller of A.

Here is some really rough pseudocode, but it should get the idea across.

PROCEDURE A
    CURSOR
        CALL B -- I WANT TO SUPPRESS THE RESULTS FROM B
    END
    SELECT *
END
PROCEDURE B
    Do some interesting things
    SELECT *
END

As you can see above, A calls B and B does some things that I want and returns results that I don't care about. Once A is done, it returns its own set of results.

How do I suppress the results from B in A? I'm using SQL Server 2005. I would prefer not to make changes to B because it is working and more complex than I want to mess with.


回答1:


You can try something like this:

/* Assume this table matches the output of your procedure */
DECLARE @tmpNewValue TABLE (newvalue int)
INSERT INTO @tmpNewValue 
EXEC ProcedureB



回答2:


Am I being really really stoopid but shouldn't "Do some interesting things" be in another procedure? Then Procedure A would call procedure C (which only does "Do some interesting things") and then do its required select and Procedure B could also call procedure C and do its select, rather than having the overhead of a second select and a temporary table that is only used as a dustbin?




回答3:


Here's a light modification of proc A & B that would suit your needs:

PROCEDURE A
    CURSOR
        CREATE TABLE #__suppress_results (col1 int)
        CALL B -- I WANT TO SUPPRESS THE RESULTS FROM B
    END
    SELECT *
END
PROCEDURE B
    Do some interesting things
    IF OBJECT_ID('tempdb..#__suppress_results') IS NULL BEGIN
      SELECT *
    END
END

This avoids the problem of nested INSERT...EXEC, and preserves the existing behavior of proc B. On the off chance that some other calling proc already creates a temp table named #__suppress_results before calling proc B, just use a different name, like #__@supress_results.

And it works, because #__suppress_results is visible within proc B if proc B is called from proc A.



来源:https://stackoverflow.com/questions/571670/how-do-i-suppress-the-results-from-a-stored-procedure-from-within-a-stored-proce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!