I have the following table:
CREATE TABLE [RTS].[MFB]
(
[record_id] [int] IDENTITY(1,1) NOT NULL,
[marker_id] [nvarchar](50) NULL,
[lat] [numeric](38, 8) NULL,
[lng] [numeric](38, 8) NULL,
[address] [nvarchar](512) NULL,
[hash] [smallint] NULL,
[updated] [datetime] NULL,
[first_created_date] [datetime] NULL,
CONSTRAINT [PK_MFB_1]
PRIMARY KEY CLUSTERED ([record_id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
where the "record_id" is the primary key.
I need to create a trigger after the INSERT operation.
The conditions are:
- If the
marker_idcolumn is new,INSERTthe record to the table and set thehashcolumn to 0; - If the
marker_idalready exists,UPDATEthe existing record by setting the newupdatedcolumn; - If both the
marker_idalready exists and any of the "lat", "lng" and "address" has been changed,UPDATEthe existing record by setting the new "lat", "lng" and/or "address" and also setting "hash" to "1".
Basically, the MFB table should not have duplicated marker_id.
How can I achieve this by a setting up a trigger? Thanks!
LOGtable) based on the event on this table.marker_idcolumn and make sure changes to this table only occur through a stored procedure that enforces these business rules. Regardless, you need to show what you've already tried - no one will write your code for you.