Solved

Update Column Using INSERT trigger

Posted on 2014-03-10
3
442 Views
Last Modified: 2014-03-10
Hi All,

I really struggle when it comes to writing triggers so I figured I would ask the experts.

I have a table called "group_members" which has the following schema:

group_member_id PK int
group_id int FK to table "groups"
contact_id int FK to table "contacts"

Whenever a record is inserted into this table I want to do the following

Query the groups table for the ID of the group inserted above and retrieve the value groups.pricing_level

Then, update the field, contacts.pricing_level in the contacts table.

In other words, we add a member to a group, retrieve the pricing level for the group and update to contact to have the same level.

Any help appreciated.  Trigger contexts always baffles me.
0
Comment
Question by:ccleebelt
3 Comments
 
LVL 6

Expert Comment

by:c1nmo
ID: 39919143
Would that not create redundancy, what if the groups.pricing_level value changed in the future?  Maybe better to retrieve the value dynamically when needed?
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39919205
CREATE TRIGGER group_members__TRG_INSERT
ON dbo.group_members
AFTER INSERT
AS
SET NOCOUNT ON
UPDATE c
SET c.pricing_level = g.pricing_level
FROM inserted i
INNER JOIN dbo.groups g ON
    g.group_id = i.group_id
INNER JOIN dbo.contacts c ON
    c.contact_id = i.contact_id
GO
0
 

Author Closing Comment

by:ccleebelt
ID: 39919405
Once again, you are the man.  Thank you.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get the latest status 8 32
SQL - Copy data from one database to another 6 19
Passing Parameter to Stored Procedure 4 24
SQL Server Error 21 8 25
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question