I am creating a stored procedure, which takes a number of input parameters. I would like to be able to select which database to use, whereafter depending on the conditions as shown below, I would like to use the correct where conditions.
I know you cannot use "USE" keyword in a stored proc and I have also tried to set @ds in the query but with not much success.
I'm using Navicat and have defined my parameters in the IDE, my input params are :
- icustomername varchar(255)
- ipostcode varchar(255)
- iaccountnumber varchar(255)
- imemberdbname varchar(255)
SQL
BEGIN
declare dbName varchar(255); --attempt at using db name passed in
select imemberdbname into dbName;
set @ds = concat(dbName,'.customers'); -- I have also tried this
if LENGTH(icustomername) > 0 THEN
(Select customerid, customername, postcode, accountnumber from dbName.customers
WHERE customername = icustomername);
ELSEIF len(ipostcode) > 0 THEN
(Select customerid, customername, postcode, accountnumber from dbName.customers
WHERE postcode = ipostcode);
ELSE
(Select customerid, customername, postcode, accountnumber from dbName.customers
WHERE accountnumber = iaccountnumber);
end if;
END
I am unable to select the db.table in order to carry out the selects. Is there a way this is possible? or is there a better solution?