How to get recordset from internal call to stored procedure?

前端 未结 2 550
情深已故
情深已故 2020-12-07 05:28

I have a stored procedure, internally I want to call another procedure that returns a record set, how do I get an navigate the record set returned by the stored procedure vi

相关标签:
2条回答
  • 2020-12-07 06:02

    internally I want to call another procedure that returns a record set,

    In your inner procedure create a TEMPORARY TABLE and populate that temp table saying insert into your_temp_table select query. then you can use that same temp table in your outer query anywhere.

    It can even be a normal table as well and need not be temporary table. Also make sure to DROP the table once your procedure computation done as clean-up.

    That's wrong per your comment. You should do it like below (a sample code)

    create procedure rsHeadOfAnyDepartments(vcCompKey varchar(10), biWho_id int)
    as
    begin
    DROP TEMPORARY TABLE IF EXISTS tbl_HeadOfDepts; 
    CREATE TEMPORARY TABLE tbl_HeadOfDepts(col1 int, col2 varchar(10), col3 varchar(30));
    
    INSERT INTO tbl_HeadOfDepts
    SELECT col1, col2, col3
    FROM tblTest;    
    end
    
    0 讨论(0)
  • 2020-12-07 06:28

    No, stored procedures can produce result sets, but not consume them directly as output from inner calls to other stored procedures. The best that you can do performance-wise is to populate a non-temporary work table and use the results.

    Depending on your software and the reality of multiple callers concurrently, you might need to include a session id concept with an auto_increment (AI) column in some control table. This would ensure that with concurrency, multiple callers are not stomping on each other's rows, thus making it non-viable.

    How that session would work, at a high level, is the following. The inner stored proc would be handed an AI value (theSession) from the control table, use it to populate a safely segmented session in the work table, and return as an out parameter to the outer (calling) stored proc. That outer one could then safely use those rows, and clean up at the end (delete from workTable where sessionId=theSession).

    Why do I suggest a non-temporary work table? To be clear, the work table would be non-temporary. First of all there is the hassle of getting the if exists drop to work. Most importantly, though, it is about performance. DDL calls for temporary table creation are not inexpensive. You will only believe this when you do performance testing to see what I mean. It may seem trivial, but in trivial operations, those DDL calls for creation could very well account for the lion share of the time necessary for the inner stored proc to complete.

    0 讨论(0)
提交回复
热议问题