Solved

SQL Server 2005 Insert Trigger or Instead of Insert Trigger

Posted on 2009-04-06
1
820 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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

695 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