如何得到存储过程的输出参数?
这是返回一个表的记录总数的存储过程
如何得到记录集总数。
CREATE procedure recordCount
(
@strWhere nvarchar(500)= ' '
)
as
declare @flag int
declare @sqlStr nvarchar(1000)
set @flag = 0
if @strWhere != ' '
set @sqlStr = 'select count(id) from get_zhaoshang where 1=1 ' + @strWhere
else
set @sqlStr = 'select count(id) from get_zhaoshang '
exec sp_executesql @sqlStr,N '@count int output ',@flag output
GO
--------------------------------
我的asp调用:
Set MyComm = Server.CreateObject( "ADODB.Command ")
MyComm.ActiveConnection = objconn
'MyConStr是数据库连接字串
MyComm.CommandText = "recordCount "
'指定存储过程名
MyComm.CommandType = 4
'表明这是一个存储过程
MyComm.Prepared = true
'要求将SQL命令先行编译
'声明参数
MyComm.Parameters.append MyComm.CreateParameter( "@strWhere ",200,1,500,SearchChar1)
MyComm.Parameters.append MyComm.CreateParameter( "@count ",3,2)
MyComm.Execute
' '取得出参
response.Write MyComm.Parameters( "@flag ").value
Set MyComm = Nothing
报错:
Microsoft OLE DB Provider for SQL Server 错误 '80040e14 '
为过程或函数 recordCount 指定的参数太多。
------解决方案--------------------CREATE procedure recordCount
(
@strWhere nvarchar(500)= ' ',
@count int output /*增加此参数*/
)
as
declare @flag int
declare @sqlStr nvarchar(1000)
set @flag = 0 /*此变量无用*/
if @strWhere != ' ' /*修改SQL内容*/
set @sqlStr = N 'select @COUNT = count(id) from get_zhaoshang where 1=1 ' + @strWhere
else /*修改SQL内容*/
set @sqlStr = N 'select @COUNT = count(id) from get_zhaoshang '
exec sp_executesql @sqlStr,N '@count int output ',@count output
GO
--------------------------------
我的asp调用:
...
'声明参数
MyComm.Parameters.append MyComm.CreateParameter( "@strWhere ",200,1,500,SearchChar1)
MyComm.Parameters.append MyComm.CreateParameter( "@count ",3,2) '注意:要把@count参数方向声明为output
MyComm.Execute
' '取得出参 /*把@flag换成@count.*/
response.Write MyComm.Parameters( "@count ").value
...
------解决方案--------------------N是National的所写,在SQLSERVE中用于表示UNICODE字符常量.sp_executesql 这个存储过程要求前二个参数类型必须是UNICODE类型的,所以字符串常量前必须加N前缀表示是UNICODE字符.