存储过程中。
我如何得到这样的记录总数?
如,平时我直接写数据库语句是:select count(sID) from test where a>1
但是我在存储过程,因为像上面的那句 sID,表test,a>1等都是传入的参数。那么我怎么 把记录总数赋给 @itemCount?
如下 提示出错
Select @itemCount = COUNT(' + @strSortColumn + ') FROM ' + @Tables + @strFilter
------解决方案--------------------
Create Table Test(ID Int Identity(1,1),a int)
Insert into Test(a) Values( 1)
Insert into Test(a) Values( 2)
Insert into Test(a) Values( 3)
Insert into Test(a) Values( 4)
Insert into Test(a) Values( 5)
Insert into Test(a) Values( 6)
Go
Declare @Sql NVarchar(400),@RecordCount Int,@TableName Varchar(80) ,@SQLWhere Varchar(80)
Set @TableName= ' Test '
Set @SQLWhere = ' a > 1'
Set @Sql= N'Select @RecordCount=count(*) From Test'
Exec Sp_executesql @Sql, [email protected] Int Out ',@RecordCount Out
Select @RecordCount as Num
Drop Table Test
Go
------解决方案--------------------
我写个示例:
[code=sql]
use pubs
--下面建一个存贮过程
create proc tmp
as
declare @i int
select @i=count(1) from sales
return @i
--使用举例
declare @j as int
exec @j=tmp
print @j --这里就是记录数呀
[/code]
------解决方案--------------------
我写个示例:
- SQL code
use pubs--下面建一个存贮过程create proc tmpasdeclare @i intselect @i=count(1) from salesreturn @i--使用举例declare @j as intexec @j=tmpprint @j --这里就是记录数呀
------解决方案--------------------
- SQL code
use pubs--下面建一个存贮过程create proc tmpasdeclare @i intselect @i=count(1) from salesreturn @i--使用举例declare @j as intexec @j=tmpprint @j --这里就是记录数呀
------解决方案--------------------
1,可以利用 sp_executesql获得参数. 写法1楼写了,我就不重复了.
2,[email protected],[email protected]出
比如
exec('declare @itemcount int;select @itemcount=count(' + @strSortColumn + ') from ' + @tables + ' ' + @strFileter + ';select @itemcount')
那么前台程序调用存储过程后, [email protected],而是做为记录集
如果用 ado 就是 nextrecordset
用 ado.net 比如 sqldatareader 就 nextresult