September 27, 2021 at 8:57 am
I have a view in my database where data is stored, it has 2 columns (ID and value) and 3 rows (ID1, 2 and 3), the ID never changes but the value column of this ID always has a new value (e.g. ID : 1 value : False; ID :2 value : 55), the ID1 always changes from True to False and ID2 and 3 always have new values
I have created a new table with 2 columns (ID and Value).
I want to write a program that every time the value of ( ID 1 ) changes from False to True in the view, a new row is automatically inserted in the new table with the value of ID2 and ID3).
this is my program :
INSERT INTO Table1 ( ID,Value )
SELECT dbo.View1.Value , View_1.Value ,
FROM dbo.View1 CROSS JOIN
dbo.View1 AS View1_1
WHERE (dbo.View1ID = 2)AND (View1_1.ID = 3)
it does what I want, but only when I run the program, I want this insert function to happen automatically every time the ID1 in the view changes from false to true. can you please help me
Translated with http://www.DeepL.com/Translator (free version)
September 27, 2021 at 9:27 am
You will need to look at a DML trigger, after update if the value of val for ID is true do an insert, else do nothing.
Something similar to the below
CREATE TRIGGER <InsertNameHere> ON <InsertTableNameHere>
AFTER UPDATE
AS
INSERT INTO <WhateverTable>
SELECT value1, value2, value3
FROM Inserted
WHERE ID1 = 1 and ID1Value = 'True'
September 27, 2021 at 1:26 pm
This was answered when you posted the question previously: https://www.sqlservercentral.com/forums/topic/add-values-from-a-table-to-an-another-table#post-3932190
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply