我想从PHP执行存储过程。我的其他SP正常工作。但是这个会产生问题。
以下是我调用SP的代码:
$sqlStmt = "EXEC MOBILE_CSKLIENT_LoadForSync @pCEGERAETNR = ?";
$con = DatabaseManager::GetDatabaseConnection($serial);
$deviceNr = $this->GetDeviceNr($serial, $deviceId);
$res = sqlsrv_query($con, $sqlStmt, array($deviceNr));
if($res == false)
{
die( print_r( sqlsrv_errors(), true));
}
else
{
die( print_r( sqlsrv_errors(), true));
$result = array();
while($zeile = sqlsrv_fetch($res))
{
echo sqlsrv_get_field($res, 0);
}
}
这是我的SP:
GO
/****** Object: StoredProcedure [dbo].[MOBILE_CSKLIENT_LoadForSync] Script Date: 25.02.2014 08:36:01 ******/
ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[MOBILE_CSKLIENT_LoadForSync](@pCEGERAETNR FLOAT)
AS
BEGIN
DECLARE @pLASTCHANGE datetime
SET @pLASTCHANGE = (SELECT MAX(LASTCHANGE) FROM CEMITARB WHERE CEGERAETNR = @pCEGERAETNR)
IF @pLASTCHANGE IS NULL
BEGIN
SELECT K.KLIENTNR, K.PROGRAMMNR, K.NAME,
K.VORNAME, K.[STR], K.PLZ, K.ORT , K.TELEFON1, K.TELEFON2, K.GEBDAT, K.GESCHLECHT, K.KEYKLIFR1, K.MAINVERSION
FROM CSKLIENT K
INNER JOIN S1PAT P ON P.KLIENTNR = K.KLIENTNR
INNER JOIN CSKLIZR Z ON Z.KLIENTNR = K.KLIENTNR
WHERE P.AKTIV = 'J'
AND (Z.AUFNAHME IS NULL OR Z.AUFNAHME <= GetDate())
AND (Z.ENTLASSUNG IS NULL OR Z.ENTLASSUNG >= GetDate())
UNION
SELECT K.KLIENTNR, K.PROGRAMMNR, K.NAME,
K.VORNAME, K.[STR], K.PLZ, K.ORT , K.TELEFON1, K.TELEFON2, K.GEBDAT, K.GESCHLECHT, K.KEYKLIFR1, K.MAINVERSION
FROM CSKLIENT K
INNER JOIN H1BEW B ON B.KLIENTNR = K.KLIENTNR
WHERE B.AKTIV = 'J' AND ZIMMER IS NOT NULL AND BEREICH IS NOT NULL
END
ELSE
BEGIN
SELECT K.KLIENTNR, K.PROGRAMMNR, K.NAME,
K.VORNAME, K.[STR], K.PLZ, K.ORT , K.TELEFON1, K.TELEFON2, K.GEBDAT, K.GESCHLECHT, K.KEYKLIFR1, K.MAINVERSION
FROM CSKLIENT K
INNER JOIN S1PAT P ON P.KLIENTNR = K.KLIENTNR
INNER JOIN CSKLIZR Z ON Z.KLIENTNR = K.KLIENTNR
WHERE P.AKTIV = 'J' AND K.LASTCHANGE >= @pLASTCHANGE
AND (Z.AUFNAHME IS NULL OR Z.AUFNAHME <= GetDate())
AND (Z.ENTLASSUNG IS NULL OR Z.ENTLASSUNG >= GetDate())
UNION
SELECT K.KLIENTNR, K.PROGRAMMNR, K.NAME,
K.VORNAME, K.[STR], K.PLZ, K.ORT , K.TELEFON1, K.TELEFON2, K.GEBDAT, K.GESCHLECHT, K.KEYKLIFR1, K.MAINVERSION
FROM CSKLIENT K
INNER JOIN H1BEW B ON B.KLIENTNR = K.KLIENTNR
WHERE B.AKTIV = 'J' AND ZIMMER IS NOT NULL AND BEREICH IS NOT NULL
AND K.LASTCHANGE >= @pLASTCHANGE
END
END
所以我现在得到错误:
Error: [Microsoft][SQL Server Native Client 10.0][SQL Server]Warnung: Ein NULL-Wert wird durch einen Aggregat- oder sonstigen SET-Vorgang gel\224scht.
翻译成英文:
A Null value will be delete by a aggregat or a set action
如果我从SQL Management Studio执行它,我的程序工作正常。 过程应返回222行。 我试着设置:
sqlsrv_configure("WarningsReturnAsErrors", 0);
但是程序没有返回任何内容。
答案 0 :(得分:1)
我找到了解决方案:
问题出在我的SP中这一行:
SET @pLASTCHANGE = (SELECT MAX(LASTCHANGE) FROM CEMITARB WHERE CEGERAETNR = @pCEGERAETNR)
LastChange可以为Null,这就是问题所在。 所以我将代码更改为:
IF((SELECT DISTICT LASTCHANGE FROM CEMITARB WHERE CEGERAETNR = @pCEGERAETNR) IS NULL)
BEGIN
SET @pLASTCHANGE = NULL
END
ELSE
BEGIN
SET @pLASTCHANGE = (SELECT MAX(LASTCHANGE) FROM CEMITARB WHERE CEGERAETNR = @pCEGERAETNR)
END
现在我的代码执行得很好。 sqlsrv_query返回正确的项目。