Solved

SQL Server 2005 Insert Trigger or Instead of Insert Trigger

Posted on 2009-04-06
1
812 Views
Last Modified: 2012-05-06
I have a table, Email, that holds email addresses, and another table, emailmessage, that holds email messages.  The tables do not have any relationship keys.  

For any new record inserted into emailmessage, first check if the values being inserted into fields "toemailaddress" OR "fromemailaddress" exist in table Email.  If yes, then do the insert.  If no, then don't.

I would like to use an Instead of Insert trigger and only commit if the condition is met.  Or, if not possible, do the check after insert and delete the row if the condition is not met.
0
Comment
Question by:worldfear
1 Comment
 

Accepted Solution

by:
worldfear earned 0 total points
Comment Utility
CREATE TRIGGER tri_EmailMessage on dbo.EmailMessage
INSTEAD OF INSERT
AS
DECLARE @no_of_rows int
DECLARE @email varchar(100)
SELECT EMAILID INTO #t1
FROM DBO.EMAIL
WHERE EMAIL IN (SELECT TOEMAIL FROM INSERTED)
SELECT @no_of_rows = @@ROWCOUNT
SELECT @email = (select toemail from inserted)

IF (@no_of_rows > 0)
INSERT INTO EmailMessage (toemail, emailmessage, hasattachment,senderemailaddress,cc,receivedtime,senton,subject)
VALUES (@email, null, null, null, null, null, null, null)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

771 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

10 Experts available now in Live!

Get 1:1 Help Now