I need some assistance with creating a stored procedure/query (doesn't really matter as long as it works) where the following happens when I insert a new row. It is a very simple table with 4 columns that relates to other tables
CREATE TABLE [#loginid_name](
[Login_ID] [int] NOT NULL,
[Login_ID_Name] [varchar](30) COLLATE Latin1_General_CI_AI NOT NULL,
[Date_Begin] [datetime] NOT NULL,
[Date_Finish] [datetime] NULL)
- When I insert a new row: query checks to see if there are other records of that ID with a higher begin_date (doesn't matters if there are multiple rows with higher begin_date, I just need the one closer to the one inserted)
- If there is, then set that date_finish of the new row as the date_begin of the existing row
- If there isn't, set the newly inserted date_finish as NULL
- Check to see if there are other records of that ID with a lower date_begin, but higher (or NULL) date_finish
- If there is, update that record's date_finish to the newly inserted date_begin value.
Does that make sense?
I can put the pieces together in my mind, but can't seem a way to code this into a single query/stored procedure in SQL. I will be inserting those rows from Excel using VBA so I can hard code this into my insert query on VBA.
BTW I would like to avoid triggers as I've tried them in the past and they give me a lot of trouble. I'm quite new at this SQL thing.
Can you help me?
Thanks!