Link to home
Start Free TrialLog in
Avatar of cvservices
cvservicesFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Chris Luttrell
Chris Luttrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cvservices

ASKER

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 ?
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.
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!
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.