存储过程从MS SQL Server进行测试并给出预期输出(对于@AID)。但是当它从PHP调用时@AID总是给出0.我在这里做错了什么?
$stmt = mssql_init('UpdateActivity2', $conn);
mssql_bind($stmt, '@ParentActivity', $PP_ActivityID, SQLINT4, false, false, 8);
mssql_bind($stmt, '@ActivityName', $activityName, SQLVARCHAR, false, false, 255);
mssql_bind($stmt, '@ActivityType', $activityType, SQLINT4, false, false, 4);
mssql_bind($stmt, '@ActivityStatus', $activityStatus, SQLINT4, false, false,4);
mssql_bind($stmt, '@Descr', $descr, SQLVARCHAR, false, false, 255);
mssql_bind($stmt, '@RegistrationDate', $now, SQLVARCHAR, false, false, 255);
mssql_bind($stmt, '@FinishDate', $now, SQLVARCHAR, false, false, 255);
mssql_bind($stmt, '@Owner', $owner, SQLINT4, false, false, 4);
mssql_bind($stmt, '@SourceID', $sourceID, SQLINT4, false, false, 4);
mssql_bind($stmt, '@ContactID', $c, SQLINT4, false, false, 4);
mssql_bind($stmt, '@ActivityAddress',$a, SQLVARCHAR, false, false, 255);
mssql_bind($stmt, '@ExtReference', $e, SQLVARCHAR, false, false, 255);
mssql_bind($stmt, '@ResponsibleCompanyID', $e, SQLINT4, false, false, 4);
mssql_bind($stmt, '@AID', $h, SQLINT4, true);
$proc_result = mssql_execute($stmt);
echo $h;
@AID是存储过程的输出参数。但是,$ h总是给出0。 这是SP:
ALTER PROCEDURE [dbo].[UpdateActivity2]
@ParentActivity int,
@ActivityName varchar(150),
@ActivityType int,
@ActivityStatus int,
@Descr varchar(1024),
@RegistrationDate datetime,
@FinishDate datetime,
@Owner int,
@SourceID int,
@ContactID int,
@ActivityAddress varchar(150),
@ExtReference varchar(50),
@ResponsibleCompanyID int,
@AID int OUTPUT
AS
DECLARE @Value int;
EXEC @Value = PG_GetSeq @Name = 'Activity'
PRINT('Printing')
PRINT(@Value)
INSERT INTO Activity(
ActivityID,
ParentActivity,
ActivityName,
ActivityType,
ActivityStatus,
Descr,
RegistrationDate,
FinishDate,
Owner,
SourceID,
ContactID,
ActivityAddress,
ExtReference,
ResponsibleCompanyID
)
VALUES(
@Value,
@ParentActivity,
@ActivityName,
@ActivityType,
@ActivityStatus,
@Descr,
@RegistrationDate,
@FinishDate,
@Owner,
@SourceID,
@ContactID,
@ActivityAddress,
@ExtReference,
@ResponsibleCompanyID
)
SELECT @AID = ActivityID FROM Activity WHERE ActivityID = @Value
Insert语句为@Value获取正确的值,并且也正确插入了行。
答案 0 :(得分:0)
如果存储过程返回参数或返回值,则在调用mssql_execute()之后这些参数或返回值将可用,除非存储过程返回多个结果集。在这种情况下,使用mssql_next_result()来转换结果。处理完最后一个结果后,输出参数和返回值将可用。
您的打印语句(也可能是行数)会干扰结果集,因此看起来好像您有多个,因此您需要调用mssql_next_result()
,直到它返回false然后您将有权访问到输出参数。
或者,您可以将true
传递给参数$skip_results
:
mssql_execute($stmt, false);
这使您可以立即访问输出参数。