![](/img/trans.png)
[英]How to get result from MSSQL stored procedure with temp table in PHP Codeigniter
[英]Get Result from Stored procedure MySql PHP in HTML Table
我已經在數據庫中存儲了過程:
DELIMITER $$
USE `billing`$$
DROP PROCEDURE IF EXISTS `Pivot`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Pivot`(
IN tbl_name VARCHAR(99), -- table name (or db.tbl)
IN base_cols VARCHAR(99), -- column(s) on the left, separated by commas
IN pivot_col VARCHAR(64), -- name of column to put across the top
IN tally_col VARCHAR(64), -- name of column to SUM up
IN where_clause VARCHAR(99), -- empty string or "WHERE ..."
IN order_by VARCHAR(99) -- empty string or "ORDER BY ..."; usually the base_cols
)
DETERMINISTIC
SQL SECURITY INVOKER
BEGIN
-- Find the distinct values
-- Build the SUM()s
SET @subq = CONCAT('SELECT DISTINCT ', pivot_col, ' AS val ',
' FROM ', tbl_name, ' ', where_clause, ' ORDER BY 1');
-- select @subq;
SET @cc1 = "CONCAT('SUM(IF(&p = ', &v, ', &t, 0)) AS ', &v)";
SET @cc2 = REPLACE(@cc1, '&p', pivot_col);
SET @cc3 = REPLACE(@cc2, '&t', tally_col);
-- select @cc2, @cc3;
SET @qval = CONCAT("'\"', val, '\"'");
-- select @qval;
SET @cc4 = REPLACE(@cc3, '&v', @qval);
-- select @cc4;
SET SESSION group_concat_max_len = 10000; -- just in case
SET @stmt = CONCAT(
'SELECT GROUP_CONCAT(', @cc4, ' SEPARATOR ",\n") INTO @sums',
' FROM ( ', @subq, ' ) AS top');
SELECT @stmt;
PREPARE _sql FROM @stmt;
EXECUTE _sql; -- Intermediate step: build SQL for columns
DEALLOCATE PREPARE _sql;
-- Construct the query and perform it
SET @stmt2 = CONCAT(
'SELECT ',
base_cols, ',\n',
@sums,
',\n SUM(', tally_col, ') AS Total'
'\n FROM ', tbl_name, ' ',
where_clause,
' GROUP BY ', base_cols,
'\n WITH ROLLUP',
'\n', order_by
);
SELECT @stmt2; -- The statement that generates the result
PREPARE _sql FROM @stmt2;
EXECUTE _sql; -- The resulting pivot table ouput
DEALLOCATE PREPARE _sql;
-- For debugging / tweaking, SELECT the various @variables after CALLing.
END$$
DELIMITER ;
當我傳遞一個SQL語句時,我想捕獲帶有列標題的行中的最終結果,並將其傳遞給PHP上的HTML Table輸出。
CALL Pivot('production','product_name','market','forcast_qty',\\“ WHERE production_date ='2015-12-31'\\”,'');
到目前為止,當我對以下過程進行編碼以獲取輸出時,我正在獲取數組,而不是存儲過程的最終結果。
if(isset($_POST['planned_forecast']))
{
if(isset($_POST['production_date'])){ $date_prod = $_POST['production_date']; }
$stmt = $DB_con->prepare("CALL Pivot('production', 'product_name', 'market', 'forcast_qty', \"WHERE production_date = '2015-12-31'\", '');");
$stmt->execute();
while ($row = $stmt->fetchAll(PDO::FETCH_ASSOC))
{
print_r($row);
}
echo '
<thead>';
echo '
</thead>
';
echo '
<tbody>
';
echo '
<tr>
';
echo '
<td></td>
';
echo '
</tr>
';
echo '
</tbody>
';
echo '</table>';
echo '<input hidden="true" name="date_prod" type="text" value="'.$date_prod.'"/>';
echo '<button type="submit" class="btn btn-default waves-effect waves-light" name="btn-editforcast" id="btn-editforcast">Update</button>';
}
結果:
數組([0] =>數組([@stmt] => SELECT GROUP_CONCAT(CONCAT('SUM(IF(market =','“',val,'”',',forcast_qty,0))AS',' “',val,'”'')分隔符“,”)INTO @sums FROM(選擇不同的市場AS VAL FROM production WHERE production_date ='2015-12-31'ORDER BY 1)AS top))
我認為您可以簡化存儲過程,並使用以下方法:
DELIMITER $$
USE `billing`$$
DROP PROCEDURE IF EXISTS `Pivot`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Pivot`(
IN tbl_name VARCHAR(99), -- table name (or db.tbl)
IN base_cols VARCHAR(99), -- column(s) on the left, separated by commas
IN pivot_col VARCHAR(64), -- name of column to put across the top
IN tally_col VARCHAR(64), -- name of column to SUM up
IN where_clause VARCHAR(99), -- empty string or "WHERE ..."
IN order_by VARCHAR(99) -- empty string or "ORDER BY ..."; usually the base_cols
)
DETERMINISTIC
SQL SECURITY INVOKER
BEGIN
-- Prepare the "pivot" part of the query
SET @stmt = CONCAT(
"SELECT GROUP_CONCAT(\" SUM(\" ",
" \" CASE WHEN ", pivot_col, "='\", ", pivot_col, ", \"'\" ",
" \" THEN ", tally_col, "\" ",
" \" ELSE 0\" ",
" \" END\" ",
" \") AS '\", ", pivot_col, ", \"'\n\") as stmt FROM (select distinct ", pivot_col, " FROM ", tbl_name, ") A INTO @sum_clause"
);
PREPARE _sql FROM @stmt;
EXECUTE _sql;
DEALLOCATE PREPARE _sql;
-- Prepare and execute the query itself
SET @stmt = CONCAT(
"SELECT ", base_cols, ", ",
@sum_clause,
", SUM(", tally_col, ") as 'Total qty'"
" FROM ", tbl_name, " \n" ,
where_clause, "\n",
" GROUP BY ", base_cols
);
PREPARE _sql FROM @stmt;
EXECUTE _sql;
DEALLOCATE PREPARE _sql;
END$$
DELIMITER ;
我不能保證它適用於所有情況,但是它適用於示例中使用的參數,並且產生的結果與原始問題中的結果相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.