1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| CREATE TABLE [dbo].[IndexHistory](
[pkId] [int] NOT NULL,
[CounterId] [int] NOT NULL,
[IndexValue] [decimal](18, 4) NOT NULL,
[Date] [datetime] NOT NULL,
[OperatorId] [int] NULL,
[pictureUrl] [varchar](200) NULL,
[Remark] [varchar](200) NULL,
[isDisabled] [bit] NULL,
PRIMARY KEY CLUSTERED
(
[pkId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
CREATE TABLE [dbo].[IndexHistoryLog](
[pkId] [int] NOT NULL,
[CounterId] [int] NOT NULL,
[IndexValue] [decimal](18, 4) NOT NULL,
[Date] [datetime] NOT NULL,
[OperatorId] [int] NULL,
[pictureUrl] [varchar](200) NULL,
[Remark] [varchar](200) NULL,
[isDisabled] [bit] NULL,
)
GO
IF OBJECT_ID(N'IO_Trig_INS_IndexHistory',N'TR') IS NOT NULL
DROP TRIGGER IO_Trig_INS_IndexHistory;
GO
CREATE TRIGGER IO_Trig_INS_IndexHistory ON IndexHistory
INSTEAD OF INSERT
AS
BEGIN
DECLARE @Date Datetime,
@CounterId int
SELECT @CounterId = inserted.CounterId,
@Date = inserted.Date
FROM inserted
IF (EXISTS (SELECT P.pkid
FROM IndexHistory P
WHERE CONVERT(Date,P.Date) = CONVERT(Date,@Date) and P.CounterId=@CounterId))
BEGIN
Insert into IndexHistoryLog
SELECT * From IndexHistory WHERE CONVERT(Date,IndexHistory.Date) = CONVERT(Date,@Date) and IndexHistory.CounterId=@CounterId
UPDATE P
SET CounterId=I.CounterId,
IndexValue=I.IndexValue,
date=getdate(),
OperatorId=I.OperatorId,
pictureUrl=I.pictureUrl,
Remark=I.Remark,
isDisabled=I.isDisabled
FROM IndexHistory P
INNER JOIN Inserted I on CONVERT(Date,P.Date) = CONVERT(Date,I.date) and P.CounterId=I.CounterId
END
ELSE
Insert into IndexHistory
SELECT * From inserted
END
GO
Insert into IndexHistory Values (1,125,15263,'2017-05-22 23:11:38.240',123,'http://','N/A',0)
Insert into IndexHistory Values (2,126,11235,'2017-05-22 23:11:39.240',123,'http://','N/A',0)
Insert into IndexHistory Values (3,125,15852,'2017-05-22 23:11:40.240',123,'http://','New entry',1)
Insert into IndexHistory Values (4,127,17562,'2017-05-22 23:11:41.240',123,'http://','N/A',0)
Insert into IndexHistory Values (5,127,17562,'2017-05-22 23:11:42.240',123,'http://','New entry',0)
Select * from IndexHistory
Select * from IndexHistoryLog |
Partager