Я создал длинный запрос для MS SQL, я проверил его вручную - он работает. Я пытаюсь сделать это с php, возвращает успех, но на самом деле запрос не выполняется.
<?php
if(isset($_POST['show'])){
$serverName = "SRV01\SIGMANEST";
$uid = "";
$pwd = "";
$databaseName = "Intranet";
$revice = "";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName,
"ReturnDatesAsStrings" => true);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$UpdateQuery = "USE [Intranet]
GO
/****** Object: View [dbo].[STEEL_UT] Script Date: 14/03/2020 23:32:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[STEEL_UT] AS
WITH PRGCOUNT AS(
SELECT
T0.Material,
T0.Thickness,
T0.Length,
T0.Width,
T0.MachineName,
T0.ProgramName,
COUNT(DISTINCT T0.ProgramName) AS 'PrNum',
COUNT(T0.ProgramName) AS 'FQty',
SUM(CAST(T0.Rep AS INT)) AS 'Rep',
AVG(CAST(T0.Yeld AS decimal)) AS 'Average'
FROM dbo.RecivePrograms T0
Where ProgramName Like '".$_POST['week']."%'
Group By T0.Material,T0.Thickness,
T0.Length,
T0.Width,
T0.MachineName,
T0.ProgramName)
SELECT
T0.Material,
T0.Thickness,
T0.Length,
T0.Width,
T0.MachineName,
CASE WHEN(SUM(CAST(T0.Rep AS INT)/T1.Fqty)) =0 THEN T1.PrNum ELSE (SUM(CAST(T0.Rep AS INT)/T1.Fqty)) END AS 'VRrep',
AVG(CAST(T0.Yeld AS decimal)) AS 'Average'
FROM dbo.RecivePrograms T0
JOIN PRGCOUNT T1 ON T0.ProgramName=T1.ProgramName
Where T0.ProgramName Like '".$_POST['week']."%'
Group By T0.Material,T0.Thickness,
T0.Length,
T0.Width,
T0.MachineName
,T0.ProgramName,
T1.PrNum
GO
";
if( $UpdateQuery === false ) {
die( print_r( sqlsrv_errors(), true));
}else {
echo "Executed";
}
$ShowTable = "SELECT [Material]
,[Thickness]
,[Length]
,[Width]
,[MachineName]
,sum([VRrep]) as 'SHITS'
,avg([Average]) as 'avg'
FROM [Intranet].[dbo].[STEEL_UT]
GROUP BY [Material]
,[Thickness]
,[Length]
,[Width]
,[MachineName]
ORDER BY Material, Thickness";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $UpdateQuery);
$stmt2 = sqlsrv_query( $conn, $ShowTable);
echo "
<div class='table-responsive'>
<table class='table table-bordered mb-4'>
<thead>
<tr>
<th>Material</th>
<th>Thickness</th>
<th>Size</th>
<th>Machine Name</th>
<th class='text-center'>Sheets</th>
<th class='text-center'>Yeld</th>
<th></th>
</tr>
</thead>
<tbody>
";
while( $row = sqlsrv_fetch_array($stmt2))
{
echo "<tr>";
echo "<Td>".$row['Material']."</td>";
echo "<Td>".$row['Thickness']."</td>";
echo "<Td>".$row['Length']."x".$row['Width']."</td>";
echo "<Td>".$row['MachineName']."</td>";
echo "<Td class='text-center'>".$row['SHITS']."</td>";
echo "<Td class='text-center'>".round($row['avg'],2)."</td>";
echo "</tr>";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt2);
sqlsrv_close( $conn);
echo"
</tbody>
</table>
</div>
";
}
?>
Процесс состоит в том, что мне сначала нужно выполнить $UpdateQuery
, чтобы обновить «временную» таблицу в SQL, который мне тогда нужно получить $ShowTable
. PHP Возвращает правду, когда я проверяю, был ли запрос выполнен правильно. Понятия не имею, почему PHP лжет.
Спасибо