Solved

SQL Server 2005 Insert Trigger or Instead of Insert Trigger

Posted on 2009-04-06
1
818 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
[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
1 Comment
 

Accepted Solution

by:
worldfear earned 0 total points
ID: 24083806
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2012 AOG and SQL2014 AOG 76 61
SQL Job Hung 17 37
Want an individual results display div 8 46
Find Patients last appointment location 4 16
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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

733 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