Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting calculated value after insert on a different table version 3

Status
Not open for further replies.

philrm

MIS
Jun 1, 1999
58
0
0
AU
Hi there

I have Three tables T1, T2 and T3. The primary key in T1 links to T2 but T2 has a different primary key. T1 also links to T3 in the same manner. IE both are 1 to many relationships. When a new entry is made in T2 I need to check if there was data entered into a particular field (F1). If there is data then I need to use the value in F1 to calculate a new record for T3 and insert it. How can I do this???? if yes, can you provide me with the code. Thanks
 
You will need what is known as an Insert Trigger on table T2. You can read about coding triggers in BOL. A trigger is basically a specialized stored procedure that SQL will execute automatically for you, whenever you Insert (in your case) a new record into T2.

(Similarly, Update and Delete triggers are also available).

Inside the trigger code, you will then write code to:

- check to see if Inserted.F1 is Not Null,
- If it is indeed Not Null, then do an Insert on the table T3.

That's a basic outline. The place to start is to read up on Triggers, in BOL, or in almost any book on programming in SQL Server.
 
Thanks bperry that was the advice I was given. What I am struggeling with is the logic of the code. I can write the simple selects and inserts but when it comes to using if else etc I fall flat. I am actually a systems admin not a programmer. Can anyone help me with the logic of what I need to write???
This more detailed look into the process may help

I have Three tables T1, T2 and T3. The primary key in T1 links to T2 but T2 has a different primary key. T1 also links to T3 in the same manner. IE both are 1 to many relationships. When a new entry is made in T2 I need to check if there was data entered into a particular field (F1). The data will NOT be a particular value but it will be an intiger. If there is data entered then I need to use the value in F1 to calculate a new values for T3 and insert them as a new record. T3 will have an at least one existing record for the primary key in T1 and I will have to select the newest of these values based on date and change them according to the value calculated from T2. T2 will also have at least one other trcord for the T1 primary key. I will need to select the new one and the one previous to the new one and subtract the old one from the new one. this value , which could be nerative will then be added to the fields in T3 How can I do this???? I was thinking that maybe it could all be done in a trigger? or would a stored procedure be better? Can anyone help me work this stuff out??
Thanks in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top