返回功能并添加其他列

时间:2014-02-17 13:43:12

标签: sql-server-2008 function cursor

我当前的表格是:

USERID  Department    NAME           actualdate
 522    xyz department  John Wayne    2/17/14 8:34
 522    xyz department  John Wayne    2/17/14 11:21
 789    xyz department  Bill Smith    2/17/14 9:41
 789    xyz department  Bill Smith    2/17/14 11:16
 789    xyz department  Bill Smith    2/17/14 11:50
 789    xyz department  Bill Smith    2/17/14 12:18
 856    xyz department  Raul Castil   2/17/14 9:25

我想创建一个允许我输入用户ID并从该用户返回数据的函数。但是,我想在第一行抓住实际日期,然后将其存储在一列中,然后检查'并且还取最后一行并将其存储在一个列中调用它'结帐'。

基本上每天我都要记录用户的入住时间,并查看员工每天工作的时间。

这是我到目前为止所拥有的"

CREATE FUNCTION [dbo].[GetEmployeeTime]
(   
@useridd int
)
RETURNS @RtnValue TABLE 
(
    useridd int,
    deptname varchar(max),
    emplname varchar(100),
    actualDate datetime,
    checkIn datetime,--first row of the actual date goes here
    checkOut datetime -- last row of the actual date goes here
)
AS
BEGIN
DECLARE  @userid int,@checkin datetime, @checkout datetime;
    DECLARE @useridAndDate   varchar(max), @useridAndDate2 varchar(max);

    DECLARE Curs CURSOR FOR 
    select ch.USERID AS useridd,ch.CHECKTIME as actualdate
    from CHECKINOUT ch
    left join USERINFO ui on ui.USERID=ch.USERID
    left join DEPARTMENTS dep on dep.DEPTID=ui.DEFAULTDEPTID

 OPEN Curs;
 FETCH NEXT FROM Curs INTO  @useridd, @actualdate;
 WHILE @@FETCH_STATUS=0

 ---so confuse here to loop through rows to record only first and last row.
--- adding userid and date so i can differentiate between dates as well as userid

 set @useridAndDate= @useridd + @actualdate
   --if @useridAndDate != @useridAndDate2
--- or if @useridd =useridd2 and @actualdate = @actualdate2
   --begin
   --end

FETCH NEXT FROM Curs INTO @useridd,@actualdate
close Curs;
deallocate Curs;
Return;

END

我希望最终结果如下:

USERID    Department       NAME    actualdate       checkin       checkout    
 522     xyz department   John Wayne    2/17/14 8:34   2/17/14 8:34   2/17/14 11:21
 789     xyz department   Bill Smith    2/17/14 9:41   2/17/14 9:41   2/17/14 12:18
 856     xyz department   Raul Castil   2/17/14 9:25   2/17/14 9:25

请帮忙

0 个答案:

没有答案