PratikGandhi
PratikGandhi

Reputation: 79

obtaining details about caller object for a procedure in MySQL

I have a procedure A which is calling another procedure B. I want to know if it is possible, in B, to get information about object that invoked it. My real purpose is to suppress the output of B if it is being called from A, else display it's output if it is invoked from outside A.

Thanks, Pratik

Upvotes: 0

Views: 143

Answers (1)

Drew
Drew

Reputation: 24959

Pratik, I think the below will be very obvious. A test section is at the bottom. procB will not render the resultset if called by procA accordingly (by the parameter). If called outside of procA, according to the parameter, it will render the resultset. So you can build your building block(s) that way.

Schema:

drop table if exists flimFlam;
create table flimFlam
(   id int auto_increment primary key,
    thing varchar(100) not null,
    calledBy varchar(100) not null,
    theWhen datetime not null
);

Two procs:

drop procedure if exists procA;
DELIMITER $$
create procedure procA()
BEGIN
    call procB('fromA');
    select 7 as seven;
END$$
DELIMITER $$

drop procedure if exists procB;
DELIMITER $$
create procedure procB(fromWhom varchar(20))
BEGIN
    insert flimFlam(thing,calledBy,theWhen) select 'blah',fromWhom,now();
    IF fromWhom != 'fromA' THEN
        select * from flimFlam;
    END IF;
END$$
DELIMITER $$

Test:

-- truncate table flimFlam;
call procB('Not from procA');
call procB('Not from procA');
call procA();
select * from flimFlam;
+----+-------+----------------+---------------------+
| id | thing | calledBy       | theWhen             |
+----+-------+----------------+---------------------+
|  1 | blah  | Not from procA | 2016-07-18 00:07:21 |
|  2 | blah  | Not from procA | 2016-07-18 00:07:29 |
|  3 | blah  | fromA          | 2016-07-18 00:07:43 |
+----+-------+----------------+---------------------+

Upvotes: 1

Related Questions