2011-08-29 122 views
1

有没有办法将sqlsrv_num_rows()与存储过程一起使用?存储过程和sqlsrv_num_rows - 光标错误?

基本上当我尝试使用此功能,我收到这些错误之一,这取决于光标i电源:

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

看到这个: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 @[email protected]@ROWCOUNT  --set output variable to be the row count 
return 0 
GO 

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

+0

太棒了,我认为这可能只是工作。谢谢! – tcarter2005