Solved

Update Column Using INSERT trigger

Posted on 2014-03-10
3
439 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:
ScottPletcher 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 46
Query Missing Money orders... 6 72
Numeric sequence in SQL 14 38
Azure SQL DB? 3 19
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now