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
Solved

SQL Server 2005 Insert Trigger or Instead of Insert Trigger

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

840 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