WEB开发网
开发学院网页设计JavaScript 存储过程中的top+变量(downmoon) 阅读

存储过程中的top+变量(downmoon)

 2010-09-14 13:33:18 来源:WEB开发网   
核心提示:存储过程中的TOP后跟一个变量会如何?Create proc getWorkPlan2(@intCounter int,@lngUserID int)asselect Top 5 lngWorkID,strWorkName,strExecHumanName,strBeginDatefrom worklist where

存储过程中的TOP后跟一个变量会如何?

Create proc getWorkPlan2
(@intCounter int
,@lngUserID int)
  
as
select Top 5 lngWorkID,strWorkName,strExecHumanName,strBeginDate
from worklist where lngExecHumanID= @lngUserID
order by lngWorkID desc

现在想将这里的Top 5 改为变量· Top @intCounter

如下

ALTER proc getWorkPlan2
(@intCounter int
,@lngUserID int)
as  
exec ('select Top '+convert(varchar(10),@intCounter)+' lngWorkID,strWorkName,strExecHumanName,strBeginDate from worklist where lngExecHumanID= '
+convert(varchar(10),@lngUserID) +' order by lngWorkID desc '

老是提示 在关键字 'convert' 附近有语法错误。

于是改为

ALTER proc getWorkPlan2
(@intCounter int
,@lngUserID int)
  
as
declare @strCounter varchar(10)
set @strCounter=convert(varchar(10),@intCounter)
declare @strUserID varchar(10)
set @strUserID=convert(varchar(10),@lngUserID)
exec ('select Top '[email protected]+' lngWorkID,strWorkName,strExecHumanName,strBeginDate from worklist where lngExecHumanID= '
[email protected] +' order by lngWorkID desc '
)

OK!

后来,经saucer(思归)大哥提醒,发现可以用以下语句实现:

Alter proc getWorkPlan2
(
@intCounter int
,@lngUserID int
)
as
set rowcount @intCounter
select lngWorkID,strWorkName,strExecHumanName,strBeginDate
from worklist where lngExecHumanID= @lngUserID
order by lngWorkID desc

Tags:存储 过程 top

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接