更新Sql Server存储过程中的问题

时间:2014-03-24 07:34:00

标签: sql sql-server stored-procedures

我在存储过程中遇到一些问题,我有一个更新2列的过程 这是我的程序:

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;

我在这个程序中的问题没有做任何更新,从我的视野来看,我没有看到结构中的任何问题,所以请帮助我在这个程序中给我正确的语法

我修改了这个结构的程序

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;

谢谢

2 个答案:

答案 0 :(得分:0)

第一个if条件中没有更新语句,这可能就是您的SP没有更新任何内容的原因。

此外,您可能希望将其他2个else if部分更改为if,但这符合您的业务要求,因此我可能错了。

编辑:如果要为变量赋值,则需要将变量保留在赋值的左侧等于。

更改以下内容

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;

以下

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;

希望这有帮助。

答案 1 :(得分:0)

感谢Guyes的帮助

这是工作和测试后的程序结构

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;