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
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.