Solved

Update Column Using INSERT trigger

Posted on 2014-03-10
3
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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 extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

752 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