Help with an SQL Trigger

Hello,
This is the first time I'm trying to create a trigger on my database, and I'm having a hard time creating it. For some reason ,I can't seem to find any clear directions on this.

In any case, I have 2 tables:
ENROLLMENT:
USE [UserNameStore]
GO

/****** Object:  Table [dbo].[Enrollment]    Script Date: 07/09/2009 11:45:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Enrollment](
      [PermID] [varchar](50) NOT NULL,
      [CN] [varchar](6) NOT NULL,
      [DTS] [datetime] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Enrollment]  WITH CHECK ADD  CONSTRAINT [FK_Enrollment_Courses] FOREIGN KEY([CN])
REFERENCES [dbo].[Courses] ([CN])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Enrollment] CHECK CONSTRAINT [FK_Enrollment_Courses]
GO

ALTER TABLE [dbo].[Enrollment]  WITH CHECK ADD  CONSTRAINT [FK_Enrollment_UserStore] FOREIGN KEY([PermID])
REFERENCES [dbo].[UserStore] ([PermID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Enrollment] CHECK CONSTRAINT [FK_Enrollment_UserStore]
GO


AND CourseRemoveQueue:
USE [UserNameStore]
GO

/****** Object:  Table [dbo].[CourseRemoveQueue]    Script Date: 07/09/2009 11:45:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[CourseRemoveQueue](
      [PermID] [varchar](50) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO



I'm attempting to create a trigger where anytime Enrollment.PermID is deleted, that same PermID is inserted into CourseRemoveQueue.PermID

and anytime a PermID is added into Enrollment.PermID, delete the corresponding CourseRemoveQueue.PermID entry.

I have attempted to create the first trigger  as such, though I'm aware that there's major flaws in my code, so I can use some help here.

CREATE TRIGGER [dbo].[Enrollment_AddRemoveQueue] ON [dbo].[Enrollment]
ON DELETE

AS
SET NOCOUNT ON;
IF EXISTS(SELECT DELETED.PermID FROM DELETED)
      INSERT INTO dbo.CourseRemoveQueue (PermID) VALUES ('DELETED.PermID')

I'm not exactly sure how to insert the just-deleted value of PermID from the Enrollment table. With the way it is right now, I get the value "DELETED.PermID" inserted into CourseRemoveQueue.Permid. Obviously that's not what I'm looking for.

TiA for any help you may be able to provide.
Georges
LVL 1
cvservicesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris LuttrellSenior Database ArchitectCommented:
I think this trigger will do what you want.
CREATE TRIGGER [dbo].[Enrollment_AddRemoveQueue] ON [dbo].[Enrollment] 
AFTER INSERT,DELETE
 
AS 
SET NOCOUNT ON;
IF EXISTS(SELECT DELETED.PermID FROM DELETED) 
      INSERT INTO dbo.CourseRemoveQueue (PermID) Select DELETED.PermID FROM DELETED
      
IF EXISTS(SELECT INSERTED.PermID FROM INSERTED) 
      DELETE FROM dbo.CourseRemoveQueue
      FROM INSERTED INNER JOIN dbo.CourseRemoveQueue ON INSERTED.PermID = dbo.CourseRemoveQueue.PermID
GO

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cvservicesAuthor Commented:
CGLuttrell. Thank you so much!
This works like a charm. Since I'm the curious type though, I was able to figure out what's happening up until the end of the first IF statement.

If it's not too much trouble, would you mind explaining the second IF statement syntax ? In particular, the 2 FROMs and the INNER JOIN ?
0
Chris LuttrellSenior Database ArchitectCommented:
You can not delete from a joined table directly like this:
      DELETE FROM dbo.CourseRemoveQueue
      INNER JOIN INSERTED ON INSERTED.PermID = dbo.CourseRemoveQueue.PermID
but you can delete from one of the joined tables by referencing it again with the "DELETE FROM tablereference" like I used
      DELETE FROM dbo.CourseRemoveQueue
      FROM INSERTED INNER JOIN dbo.CourseRemoveQueue ON INSERTED.PermID = dbo.CourseRemoveQueue.PermID

I generally build my complex delete statements by first working on a select that will identify the records that need deleted with a
SELECT *
FROM table1 inner join table2 on col1 = col2 ....
and then when that returns just what I want to delete I change the SELECT * into DELETE FROM Table1 to do the actual delete.

I hope that makes sense and answeres your question.
0
cvservicesAuthor Commented:
I think that does.
All of this is a bit overwhelming for me, because this is the first relational database that I'm designing. so some of the terminology and logic in SQL statements, especially when it's embedded in Triggers or Procedures, is hard for me to wrap my head around. I'll get there.

Thank you very much for the explanation though. It certainly helped.
The points are yours!
0
Chris LuttrellSenior Database ArchitectCommented:
Thanks, glad I could help.  I follow Zones SQL Server 2008, SQL Server 2005 and MS SQL Reporting so post any other questions you come up with and I will gladly try to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.