我的问题是,当我尝试通过PHP脚本执行SQL存储过程时,遇到数据丢失的情况。但是,当我在SQL Server Management Studio上运行相同的SP时,没有任何问题或数据丢失。
我尝试增加SQL SP,也尝试增加服务器上远程SQL执行时间。但是问题仍然存在。
我正在使用两个光标。
通过SQL Server Management Studio运行存储过程时,没有任何问题。
SQL Server 2008 R2
PHP V5.6
我需要一种解决方案来改善我的SQL存储过程或增加远程SQL执行时间。
PHP脚本
if(isset($_POST['date1'])){
$callSP = "{call SP_SelectAllEmployeeOperationDetails(?,?)}";
$params = array(
array($_POST['date1'], SQLSRV_PARAM_IN),
array($_POST['date2'], SQLSRV_PARAM_IN)
);
$stmt = sqlsrv_query( $conn, $callSP, $params, array("QueryTimeout" => 900000));
if( $stmt === false ){
echo "Error in executing statement 3.\n";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_free_stmt($stmt);
}
SQL SP
USE [ServiceMasterN]
GO
/****** Object: StoredProcedure [dbo].[SP_SelectAllEmployeeOperationDetails] Script Date: 10/05/2018 09:02:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--execute SP_SelectAllEmployeeOperationDetails '2018-10-01', '2018-10-01'
ALTER PROCEDURE [dbo].[SP_SelectAllEmployeeOperationDetails]
@DateFrom varchar (20),
@DateTo varchar (20)
as
BEGIN
DELETE FROM [TempEmpWiseBayWiseActivities]
DELETE FROM [TempOperationDetails1]
DECLARE @EmpCode varchar(10)
DECLARE @Cur1 as CURSOR;
SET @Cur1 = CURSOR FAST_FORWARD FOR
SELECT Emp_code FROM BaywiseEmployee
OPEN @Cur1;
FETCH NEXT FROM @Cur1 INTO @EmpCode;
WHILE @@FETCH_STATUS = 0
BEGIN
--PRINT 'Processing ClientID: ' + Cast(@EmpCode as Varchar);
DECLARE @Bay char(4)
DECLARE @Cur2 as CURSOR;
SET @Cur2 = CURSOR FAST_FORWARD FOR
SELECT BaCode FROM Bay WHERE BaCode<>00
OPEN @Cur2;
FETCH NEXT FROM @Cur2 INTO @Bay;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO [TempOperationDetails1]
([EmpCode],[BayCode],[Count],[FromDate],[ToDate])
SELECT @EmpCode,@Bay,COUNT(*),@DateFrom,@DateTo
FROM JobActivityWiseEmployee INNER JOIN
JobHeader ON JobActivityWiseEmployee.JobNo = JobHeader.JobNo
WHERE (JobHeader.JobDate BETWEEN @DateFrom AND @DateTo)
GROUP BY JobActivityWiseEmployee.BaCode, JobActivityWiseEmployee.Emp_code
HAVING (JobActivityWiseEmployee.BaCode = @Bay) AND (JobActivityWiseEmployee.Emp_code = @EmpCode)
FETCH NEXT FROM @Cur2 INTO @Bay;
END;
CLOSE @Cur2;
DEALLOCATE @Cur2;
FETCH NEXT FROM @Cur1 INTO @EmpCode;
END;
CLOSE @Cur1;
DEALLOCATE @Cur1;
END