I’ve asked the same question some days ago, but there I wanted to solve my problem with a separate C# program. Now I want to do it in SQL directly.
That’s my problem (Sorry for linking you to another question)
My first attempts were to create this trigger:
CREATE TRIGGER myNewTrigger ON [dbo].[myTable]
FOR INSERT
AS
INSERT INTO dbo.myTargetTable
(TIMESTAMP, VALUE_1, VALUE_2, VALUE_3, VALUE_4)
SELECT
DATEADD(SECOND, TIMESTAMP_S,'19700101'), VALUE, VALUE, VALUE, VALUE
FROM inserted
GO
This would create four rows each with the same four values. But how can I put the values of each row in one row with different columns?
Thanks for any help!
This untested Select-Statement could be your solution, if there wouldn’t be a problem with the trigger.
First problem:
The trigger is fired after every insert on mytable.
After the first insert for a Timestamp, there is just one value. So you need to update after the third or fourth record.
You do the insert for the last and not for the current timestamp. That problem is not solved in my solution.
Second problem:
You need to make shure, that you insert only once and not after every single row.
You could add a NOT IN-Clause to the select statement for the insert.
That problem isn’t solved in my solution.
Possibel Solution to your Problem, stated in the comment:
With this untested Query, you insert the last, not the current TS into the target-table. That way you make shure you just update, when the last entry of the TS is written, so you have all of the values(1..4) written, before inserting them.
It might be slow :-). But I don’t know a better way.