我有一个存储过程,如果表中不存在这些记录并且更新存在的记录,则插入新的记录。 我的问题是: 当我在每次插入新记录时执行此过程,即使该记录已经存在并且应该更新它。 该过程的唯一操作是仅插入但不更新。
这是我谈论的程序,
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;
IF @@ROWCOUNT = 0 and @Trns_Typ = 'Check In'
Insert Into [dbo].[F_ATTENDANCE] ([ATND_EMP_SRGT],
[ATND_EMP_IN_TS],[ATND_DT_SRGT],[ATND_CREATED_DATE],[ATND_TIME])
Values (@EMP_Srgt,@Trns_TS,@Dt_Srgt,Getdate(),
(convert(float,replace(left(convert(time,(@OUT_TS- @Trns_TS),103),5),':','.'))));
ELSE IF @@ROWCOUNT = 0 and @Trns_Typ = 'Check Out'
Insert Into [dbo].[F_ATTENDANCE] ([ATND_EMP_SRGT],
[ATND_EMP_Out_TS],[ATND_DT_SRGT],[ATND_CREATED_DATE],[ATND_TIME])
Values (@EMP_Srgt,@Trns_TS,@Dt_Srgt,Getdate(),
(convert(float,replace(left(convert(time,(@Trns_TS-@IN_TS ),103),5),':','.'))));
End;
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),':','.')),[ATND_LAST_MODIFIED_DATE] = getdate()
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),':','.')),[ATND_LAST_MODIFIED_DATE] = getdate()
where [ATND_EMP_SRGT] = @EMP_Srgt and [ATND_DT_SRGT] = @Dt_Srgt;
end;
End;
请帮我解决此问题,谢谢
答案 0 :(得分:0)
我说这是@@ ROWCOUNT的问题。
@@ ROWCOUNT非常不稳定,每次声明后都会发生变化。在select之后立即将@@ ROWCOUNT的值保存到int变量中,并将该变量与后续IF一起使用。看到这里的区别:
create table #t (i int, j char);
insert #t values (1, 'a'), (2, 'b');
declare @m int;
select @m = i from #t where i = 1;
if @@rowcount = 0
select '1a';
else if @@rowcount = 0
select '1b';
declare @n int;
select @n = i from #t where i = 1;
declare @rc int = @@rowcount;
if @rc = 0
select '2a';
else if @rc = 0
select '2b';
drop table #t;
在你的情况下是这样的:
**declare @rc int;**
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;
**set @rc = @@ROWCOUNT;**
IF **@rc = 0** and @Trns_Typ = 'Check In'
Insert Into [dbo].[F_ATTENDANCE] ([ATND_EMP_SRGT],
[ATND_EMP_IN_TS],[ATND_DT_SRGT],[ATND_CREATED_DATE],[ATND_TIME])
Values (@EMP_Srgt,@Trns_TS,@Dt_Srgt,Getdate(),
(convert(float,replace(left(convert(time,(@OUT_TS- @Trns_TS),103),5),':','.'))));
ELSE IF **@rc = 0** and @Trns_Typ = 'Check Out'
Insert Into [dbo].[F_ATTENDANCE] ([ATND_EMP_SRGT],
[ATND_EMP_Out_TS],[ATND_DT_SRGT],[ATND_CREATED_DATE],[ATND_TIME])
Values (@EMP_Srgt,@Trns_TS,@Dt_Srgt,Getdate(),
(convert(float,replace(left(convert(time,(@Trns_TS-@IN_TS ),103),5),':','.'))));
End;