I have the following stored procedure:
ALTER PROCEDURE SP_STOCK_ANALYSIS
(
MAIN_GROUP CHAR(6)
)
RETURNS
(
STOCK_CODE CHAR(21),
STOCK_GROUP CHAR(6),
DESCRIPTION CHAR(31),
EXPENSE NUMERIC(15, 4)
)
AS
BEGIN
FOR
SELECT
L.STOCK_CODE, INV.DESCRIPTION, INV.STOCK_GROUP, L.BALANCE
FROM
LEDGER L LEFT JOIN INVENTORY INV ON L.STOCK_CODE = INV.STOCK_CODE
WHERE
INV.STOCK_GROUP in (:MAIN_GROUP)
AND L.LEDGER_ACCOUNT in ('71212', '71211' ,'83791')
INTO
STOCK_CODE, STOCK_GROUP, DESCRIPTION, EXPENSE
DO
In the select statement I have the following three accounts:
- 71212
- 71211
- 83791
Ideally I would like to change the stored procedure so that I am able to enter the account numbers as part of the parameters. The challenge is that the number of accounts might change. Is it possible to use a string as a parameter? How would I go about doing that?
Firebird does not support a variable number of parameters to a stored procedure. However, you can define default parameter values. So, you could specify a first parameter without default, followed by multiple parameters with a default, and then call the stored procedure with 1 or more parameters.
Alternatively, you could pass a comma separated string, and use a helper stored procedure to split that string into multiple rows.
You would then do something like
With
split_groups
something like