I created a long query for MS SQL, I checked it manually - it works. I try to do it with php, return success, but in fact the query is not executed.
<?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>
";
}
?>
The process is that I have to execute $UpdateQuery first to update the 'temporary' table in SQL, which I then need to get by $ShowTable. PHP Returns the truth when I check if Query was done correctly. I have no idea why PHP lies.
Thank you
GOis not a T-SQL statement. RemoveGOand test again.