简体   繁体   中英

Update issue in Sql Server stored procedure

I have a little problem in Stored procedure, I have a procedure to making update for 2 columns This is my procedure:

CREATE PROCEDURE [dbo].[P_Attendance_CheckTime1]
@EMP_ID INT = null, /*Employee Source Code*/
@Trns_Typ nvarchar = null,/*Transaction Type('Check In','Check Out')*/
@Trns_TS Datetime = null,/*Transaction Time Stamp*/
@OUT_TS Datetime = null,/*Attendance Out Time Stamp*/
@IN_TS Datetime = null,/*Attendance In Time Stamp*/
@EMP_Srgt  INT = null, /*Employee Surrogate Key INsert it into Var.*/
@Dt_Srgt INT  = null /*Date Surrogate Key INsert it into Var.*/
AS
BEGIN
SET NOCOUNT ON;
if  @Trns_Typ in ('Check In', 'Check Out') 
    begin
    select Emp_Srgt = @EMP_Srgt 
        from [dbo].[D_EMPLOYEE] 
        where [EMP_SRC_CD] = @EMP_ID;
    select [DT_SRGT]= @Dt_Srgt 
        from d_date 
        where G_DT = replace(convert(varchar(12),@Trns_TS,112),'-','');
    select [ATND_EMP_OUT_TS] = @OUT_TS, [ATND_EMP_IN_TS] = @IN_TS 
        from [dbo].[F_ATTENDANCE]
    where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
else if @Trns_Typ = 'Check In' and (@IN_TS is null or @IN_TS > @Trns_TS  ) 
    begin
    update [dbo].[F_ATTENDANCE] 
        set [ATND_EMP_IN_TS] = @Trns_TS
    ,[ATND_TIME]=convert(float,replace(left(convert(time,([ATND_EMP_OUT_TS] - @Trns_TS),103),5),':','.'))
    where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
    end;
else if @Trns_Typ = 'Check Out' and (@OUT_TS is null or @OUT_TS < @Trns_TS ) 
    begin
     update [dbo].[F_ATTENDANCE] 
         set [ATND_EMP_OUT_TS] = @Trns_TS
     ,[ATND_TIME] = convert(float,replace(left(convert(time,(@Trns_TS - [ATND_EMP_IN_TS]),103),5),':','.'))
     where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
Print 'There Are Error In These Data'
end;

My problem in this procedure it dose not make any update, from my vision I don't see any problem in the structure, so please help me in this procedure and give me the correct syntax

I modify the procedure to this structure

CREATE PROCEDURE [dbo].[P_Attendance_CheckTime2]
@EMP_ID INT = null, /*Employee Source Code*/
@Trns_Typ nvarchar = null,/*Transaction Type('Check In','Check Out')*/
@Trns_TS Datetime = null,/*Transaction Time Stamp*/
@OUT_TS Datetime = null,/*Attendance Out Time Stamp*/
@IN_TS Datetime = null,/*Attendance In Time Stamp*/
@EMP_Srgt  INT = null, /*Employee Surrogate Key INsert it into Var.*/
@Dt_Srgt INT  = null /*Date Surrogate Key INsert it into Var.*/
AS
BEGIN
SET NOCOUNT ON;
select Emp_Srgt = @EMP_Srgt from [dbo].[D_EMPLOYEE] where [EMP_SRC_CD] = @EMP_ID;
select [DT_SRGT]= @Dt_Srgt from d_date where G_DT = replace(convert(varchar(12),@Trns_TS,112),'-','');
select [ATND_EMP_OUT_TS] = @OUT_TS, [ATND_EMP_IN_TS] = @IN_TS from [dbo].[F_ATTENDANCE]
where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
if @Trns_Typ = 'Check In' and (@IN_TS is null or @IN_TS > @Trns_TS  ) 
    begin
update [dbo].[F_ATTENDANCE] set [ATND_EMP_IN_TS] = @Trns_TS
,[ATND_TIME]=convert(float,replace(left(convert(time,([ATND_EMP_OUT_TS] - @Trns_TS),103),5),':','.'))
where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
if @Trns_Typ = 'Check Out' and (@OUT_TS is null or @OUT_TS < @Trns_TS ) 
    begin
update [dbo].[F_ATTENDANCE] set [ATND_EMP_OUT_TS] = @Trns_TS
,[ATND_TIME] = convert(float,replace(left(convert(time,(@Trns_TS - [ATND_EMP_IN_TS]),103),5),':','.'))
where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
Print 'There Are Error In These Data'
end;

Thanks

There are no update statements in the first if condition probably that is why your SP is not updating anything.

Also you might want to change the other 2 else if parts to just if but that is as per your business requirement so I might be wrong.

EDIT: If you want to assign a value to a variable you need to keep the variable on the left hand side of the assignment equals.

Change the following

if  @Trns_Typ in ('Check In', 'Check Out') 
begin
    select Emp_Srgt = @EMP_Srgt 
        from [dbo].[D_EMPLOYEE] 
        where [EMP_SRC_CD] = @EMP_ID;
    select [DT_SRGT]= @Dt_Srgt 
        from d_date 
        where G_DT = replace(convert(varchar(12),@Trns_TS,112),'-','');
    select [ATND_EMP_OUT_TS] = @OUT_TS, [ATND_EMP_IN_TS] = @IN_TS 
        from [dbo].[F_ATTENDANCE]
    where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;

to the following

if  @Trns_Typ in ('Check In', 'Check Out') 
begin
    select @Emp_Srgt = EMP_Srgt 
        from [dbo].[D_EMPLOYEE] 
        where [EMP_SRC_CD] = @EMP_ID;
    select @DT_SRGT = [Dt_Srgt]
        from d_date 
        where G_DT = replace(convert(varchar(12),@Trns_TS,112),'-','');
    select @OUT_TS = [ATND_EMP_OUT_TS], @IN_TS = [ATND_EMP_IN_TS]
        from [dbo].[F_ATTENDANCE]
    where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;

Hope this helps.

Thanks Guyes for your help

That's the structure of the procedure after working and testing

CREATE PROCEDURE [dbo].[P_Attendance_CheckTime]
@EMP_ID INT = null, /*Employee Source Code*/
@Trns_Typ nvarchar(50) = null, /*Transaction Type('Check In','Check Out')*/
@Trns_TS Datetime = null, /*Transaction Time Stamp*/
@OUT_TS Datetime = null, /*Attendance Out Time Stamp*/
@IN_TS Datetime = null, /*Attendance In Time Stamp*/
@EMP_Srgt  INT = null, /*Employee Surrogate Key INsert it into Var.*/
@Dt_Srgt INT  = null /*Date Surrogate Key INsert it into Var.*/
AS
BEGIN
SET NOCOUNT ON;
if  @Trns_Typ in ('Check In','Check Out') 
begin
    select @Emp_Srgt = EMP_Srgt 
        from [dbo].[D_EMPLOYEE] 
        where [EMP_SRC_CD] = @EMP_ID;
    select @DT_SRGT = [Dt_Srgt]
        from d_date 
        where G_DT = replace(convert(varchar(12),@Trns_TS,112),'-','');
    select @OUT_TS = [ATND_EMP_OUT_TS], @IN_TS = [ATND_EMP_IN_TS]
        from [dbo].[F_ATTENDANCE]
    where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
if (@Trns_Typ = 'Check In' and (@IN_TS is null or @IN_TS > @Trns_TS  )) 
--or (@Trns_Typ = 'Check Out' and (@OUT_TS is null or @OUT_TS < @Trns_TS ))
    begin
update [dbo].[F_ATTENDANCE] set [ATND_EMP_IN_TS] = @Trns_TS
,[ATND_TIME]=convert(float,replace(left(convert(time,([ATND_EMP_OUT_TS] - @Trns_TS),103),5),':','.'))
where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
if @Trns_Typ = 'Check Out' and (@OUT_TS is null or @OUT_TS < @Trns_TS ) 
begin
update [dbo].[F_ATTENDANCE] set [ATND_EMP_OUT_TS] = @Trns_TS
,[ATND_TIME] = convert(float,replace(left(convert(time,(@Trns_TS - [ATND_EMP_IN_TS]),103),5),':','.'))
where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM