存储过程和sqlsrv_num_rows - 游标错误?

时间:2011-08-29 19:13:57

标签: php sql-server

有没有办法在存储过程中使用sqlsrv_num_rows()?

基本上当我尝试使用此功能时,我会收到其中一个错误,具体取决于我提供的游标:

Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -50 [code] => -50 [2] => This function only works with statements that have static or keyset scrollable cursors. [message] => This function only works with statements that have static or keyset scrollable cursors. ) ) 

Array ( [0] => Array ( [0] => 01000 [SQLSTATE] => 01000 [1] => 16954 [code] => 16954 [2] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Executing SQL directly; no cursor. [message] => [Microsoft][SQL Server Native Client 10.0][SQL Server]Executing SQL directly; no cursor. ) [1] => Array ( [0] => 01S02 [SQLSTATE] => 01S02 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 10.0]Cursor type changed [message] => [Microsoft][SQL Server Native Client 10.0]Cursor type changed ) )

我花了一些时间试图寻找这个问题的解决方案,但没有运气。

如何使用SqlSrv从存储过程中获取行数?我是否必须手动循环结果和$ count ++;我想避免这种情况。或者,是否有某种方法可以更改实际的存储过程以包含光标?我对游标不太熟悉,所以我真的不知道这是否是一个解决方案。

提前谢谢!

1 个答案:

答案 0 :(得分:1)

看到这个:sqlsrv_num_rows() with stored procedures 似乎除了解决方法之外还有很多其他内容。

您可以在过程中捕获@@ ROWCOUNT并将其作为OUTPUT参数返回。

CREATE PROCEDURE YourProc
(
    @param1 int
   ,@param2 varchar(5)
   ,@rowCountOf int OUTPUT
)
AS

SELECT * FROM TABLE WHERE ...  --returns result set
SET @rowCountOf=@@ROWCOUNT     --set output variable to be the row count
return 0
GO

或者您可以使用sp_executesql代替上述链接中所述。