I have spend several days now trying to solve this problem inside a stored procedure. I want to call a procedure providing 3 variables. The variables area used to select the appropriate table from within the database. I have to concatenate the 3 variables to make up the whole table name and concatenate that with the field name.
The hard coded procedures looks as follows:
Expand|Select|Wrap|Line Numbers
- DROP PROCEDURE IF EXISTS `nebula_fit`.`Traffic_Profile` $$
- CREATE DEFINER=`root`@`localhost` PROCEDURE `Traffic_Profile`(IN sCompany CHAR(10), IN sMonth CHAR(3), IN sYear CHAR(4))
- BEGIN
- SELECT CONCAT('', sMonth, '_', sYear, '') as MonthValue,
- `usg_summaryinvoice`.`AccountNo`,
- `usg_calldetail_tel_npak_jun_2007`.`OriginatingNumber`,
- `usg_calltype_tel`.`CallType`,
- SUM(`usg_calldetail_tel_npak_jun_2007`.`CallCost`) as TotalCallCost,
- SUM(`usg_calldetail_tel_npak_jun_2007`.`CallDuration`) as TotalDuration,
- COUNT(`usg_calldetail_tel_npak_jun_2007`.`CallDetailID`) as TotalCallCount
- FROM `usg_calldetail_tel_npak_jun_2007`
- LEFT JOIN `usg_calltype_tel` ON `usg_calldetail_tel_npak_jun_2007`.`CallTypeID` = `usg_calltype_tel`.`CallTypeID`
- LEFT JOIN `usg_summaryinvoice` ON `usg_calldetail_tel_npak_jun_2007`.`InvoiceNo` = `usg_summaryinvoice`.`InvoiceNo`
- GROUP BY MonthValue,
- `usg_summaryinvoice`.`AccountNo`,
- `usg_calldetail_tel_npak_jun_2007`.`OriginatingNumber`,
- `usg_calltype_tel`.`CallTypeID`,
- `usg_calltype_tel`.`CallType`
- ORDER BY MonthValue,
- `usg_summaryinvoice`.`AccountNo`,
- `usg_calldetail_tel_npak_jun_2007`.`OriginatingNumber`,
- `usg_calltype_tel`.`CallTypeID`;
- END
So basically what I'm trying to do is something like this
Expand|Select|Wrap|Line Numbers
- SELECT CONCAT('', sMonth, '_', sYear, '') as MonthValue,
- `usg_summaryinvoice`.`AccountNo`,
- concat("`usg_calldetail_tel_", sCompany, "_", sMonth, "_", sYear, ".OriginatingNumber"),
- `usg_calltype_tel`.`CallType`,
- SUM(`usg_calldetail_tel_npak_jun_2007`.`CallCost`) as TotalCallCost,
- SUM(`usg_calldetail_tel_npak_jun_2007`.`CallDuration`) as TotalDuration,
- COUNT(`usg_calldetail_tel_npak_jun_2007`.`CallDetailID`) as TotalCallCount
- FROM concat("`usg_calldetail_tel_", sCompany, "_", sMonth, "_", sYear);
Expand|Select|Wrap|Line Numbers
- SELECT CONCAT('', sMonth, '_', sYear, '') as MonthValue,
- `usg_summaryinvoice`.`AccountNo`,
- "usg_calldetail_tel_" || sCompany || "_" || sMonth || "_" || sYear || ".OriginatingNumber"),
- `usg_calltype_tel`.`CallType`,
- SUM(`usg_calldetail_tel_npak_jun_2007`.`CallCost`) as TotalCallCost,
- SUM(`usg_calldetail_tel_npak_jun_2007`.`CallDuration`) as TotalDuration,
- COUNT(`usg_calldetail_tel_npak_jun_2007`.`CallDetailID`) as TotalCallCount
- FROM "usg_calldetail_tel_" || sCompany || "_" || sMonth || "_" || sYear || ";"
I have also tried to put it into one long string and exec that statement, but similiar problems.
Even a good article will do, because I have reached the end of the internet searching for a solution
Thx Ant