2014-03-31 203 views
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; 

請幫我解決這個問題,感謝

回答

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,(@[email protected]_TS),103),5),':','.')))); 
End; 
+0

感謝院長烏拉圭回合的答覆,但我怎麼能在我的procedurehttp使用它://stackoverflow.com/users/3464852/院長 – user3143565

相關問題