0
我有一個存儲過程,用於插入新記錄,如果這些記錄不存在於表中並更新存在的記錄。 我的問題是: 當我每次插入新記錄時執行此過程即使此記錄已存在並應更新它。 該過程的唯一操作是隻插入但不更新。在SQL Server中插入新記錄並更新現有記錄存儲過程
這就是我講的過程中,
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,(@[email protected]_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;
請幫我解決這個問題,感謝
感謝院長烏拉圭回合的答覆,但我怎麼能在我的procedurehttp使用它://stackoverflow.com/users/3464852/院長 – user3143565