如何在 sqlserver 的函数中使用行计数?
我在 sql server 2000 的函数中使用 rowcount 时遇到了问题。 它显示一个错误,例如在函数中无效使用'UNKNOWN TOKEN'。
我的函数是这样的。
ALTER Function fnc_GetOpenShiftWorkID (@EMP_ID int,@Counter int,@date Datetime) returns int as
BEGIN
SET ROWCOUNT @Counter
declare @result int
if exists(select * from tbl_org_workinghrs WHERE EMP_ID=@EMP_ID and SDATE=@DATE)
BEGIN
select @result= WORK_ID
from tbl_org_working_hrs work_hrs
inner join tbl_org_shift_group sgroup on sgroup.WH_ID=work_hrs.WORK_ID
inner join tbl_org_workinghrs workhrs on workhrs.GROUP_ID=sgroup.GROUP_ID
WHERE EMP_ID=@EMP_ID
and SDATE=@DATE
order by
IN_START
END
ELSE
BEGIN
if exists(select * from tbl_org_workinghrs where EMP_ID=0)
BEGIN
select @result=WORK_ID
from tbl_org_working_hrs
WHERE IS_DEFAULTSHIFT=1
END
END
return @result
END
I have faced a problem using rowcount in function in sql server 2000.
It shows an error like Invalid use of 'UNKNOWN TOKEN' within a function.
MY function is like this.
ALTER Function fnc_GetOpenShiftWorkID (@EMP_ID int,@Counter int,@date Datetime) returns int as
BEGIN
SET ROWCOUNT @Counter
declare @result int
if exists(select * from tbl_org_workinghrs WHERE EMP_ID=@EMP_ID and SDATE=@DATE)
BEGIN
select @result= WORK_ID
from tbl_org_working_hrs work_hrs
inner join tbl_org_shift_group sgroup on sgroup.WH_ID=work_hrs.WORK_ID
inner join tbl_org_workinghrs workhrs on workhrs.GROUP_ID=sgroup.GROUP_ID
WHERE EMP_ID=@EMP_ID
and SDATE=@DATE
order by
IN_START
END
ELSE
BEGIN
if exists(select * from tbl_org_workinghrs where EMP_ID=0)
BEGIN
select @result=WORK_ID
from tbl_org_working_hrs
WHERE IS_DEFAULTSHIFT=1
END
END
return @result
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要获取按
IN_START
排序的第 n 行的值。从 SQL Server 2005 以后,您可以使用
top(n)
或row_number()
。在 SQL Server 2000 中,您可以使用带有身份 ID 字段的表变量作为临时存储。
像这样的东西。
You want to get the value of the n'th row ordered by
IN_START
.From SQL Server 2005 later you could use
top(n)
orrow_number()
.In SQL Server 2000 you can use a table variable with an identity ID field as a temp storage.
Something like this.