How to supply a view, procedure or table name to a stored procedure?


You might have a stored procedure that does something with some table, but it is determined at run-time which table it should be. You can do this with EXECUTE STATEMENT. For example, we wish to supply the table name from which we wish to extract the values for the field ID of that table:

SET TERM !! ;

CREATE PROCEDURE p1 ( TableName VARCHAR(32))
RETURNS ( id integer )
AS
DECLARE VARIABLE stmt VARCHAR(1000);
BEGIN
stmt = 'select id from '||:TableName;
for execute statement :stmt into :id
do suspend;
END;

SET TERM ; !!

You can do similar things with views and stored procedures as well. The only thing that cannot be variable is the number and names of output parameters (fields).


Do you find this FAQ incorrect or incomplete? Please e-mail us what needs to be changed. To ensure quality, each change is checked by our editors (and often tested on live Firebird databases), before it enters the main FAQ database. If you desire so, the changes will be credited to your name. To learn more, visit our add content page.



All contents are copyright © 2007-2024 FirebirdFAQ.org unless otherwise stated in the text.


Links   Firebird   News   FlameRobin   Powered by FB: Home Inventory   Euchre  
Add content   About  

Categories
 Newbies
 SQL
 Installation and setup
 Backup and restore
 Performance
 Security
 Connectivity and API
 HOWTOs
 Errors and error codes
 Miscellaneous