ADD Values from a table to an another table

  • I am working with Microsoft SQL Server MANAGMENT STUDIO 18  and i Have 2 Tables , i want to insert a new line of values from Table 1 on Table 2 but every time that a Value From Table 1 change from False to True .Actually it works but only if i execute to This Program that i wrote

    INSERT INTO Table2 ( Datum , Zeit, Temperatur , Oxidationszeit )

    SELECT        dbo.V_Table1.Value , V_Table1.Value , V_Table1.Value ,  V_Table1.Value

    FROM            dbo.V_Table1 CROSS JOIN

    dbo.V_Table1 AS V_Table1_1 CROSS JOIN

    dbo.V_Table1 AS V_Table1_2 CROSS JOIN

    dbo.V_Table1 AS V_Table1_3

    WHERE        (dbo.V_Table1.ConfigID = 248)AND (V_Table1_1.ConfigID = 249)  AND (V_Table1_2.ConfigID = 163) AND (V_Table1_3.ConfigID = 174)

    My question is how can i add an If conditon or a trigger so that :

    every time that a Value From Table 1 change => a new Line automatically is added to Table 2 with the value that i need

    i will be grateful if someone can help me.

  • You're describing an after update trigger.

    Triggers use virtual/logical tables called deleted and inserted. "deleted" contains the data for all rows in the table affected by the transaction before the change for an update or delete. "inserted" contains the data for all rows inserted into/updated in the table.

    So to insert into table 2, in your trigger body you will select the appropriate values from inserted, and/or whatever values you need, hardcoded or from other sources.

    Note:  More than one row may be affected by a transaction. Never write trigger code that assumes there is only one row.

  • If you provide DDL (CREATE TABle) for at least Table_1 and some test data, perhaps somebody can help you write a trigger.

    Zidar's Theorem: The best code is no code at all...

  • This was removed by the editor as SPAM

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply