Solved

MS SQL Trigger

Posted on 2007-04-05
5
631 Views
Last Modified: 2008-06-28
Hey All!

Ok, new to MS SQL triggers...  SO, I need to try and do the following...

1: 2 tables SYS_USER, SERV_REQUEST, and SERV_NOTE
2: When a user is deleted from SYS_USER I need all refrences in SERV_REQUEST, and SERV_NOTE to be deleted as well.
3: I want the trigger to be on the SYS_USER table.

Here is the SQL for the tables and the joins...

SELECT
  user1229049.SYS_USER.ID_USER,
  user1229049.SYS_USER.ID_DISTRICT,
  user1229049.SYS_USER.ID_BUILDING,
  user1229049.SYS_USER.[USER_NAME],
  user1229049.SYS_USER.USER_PASSWORD,
  user1229049.SYS_USER.FIRST_NAME,
  user1229049.SYS_USER.LAST_NAME,
  user1229049.SYS_USER.EMAIL,
  user1229049.SYS_USER.RIGHTS,
  user1229049.SERV_REQUEST.ID_REQUEST,
  user1229049.SERV_REQUEST.ID_DISTRICT,
  user1229049.SERV_REQUEST.ID_BUILDING,
  user1229049.SERV_REQUEST.ID_ADMIN,
  user1229049.SERV_REQUEST.ID_TECHNICIAN,
  user1229049.SERV_REQUEST.ID_USER,
  user1229049.SERV_REQUEST.ID_PRIORITY,
  user1229049.SERV_REQUEST.ID_SUB_PRIORITY,
  user1229049.SERV_REQUEST.ID_ROOM,
  user1229049.SERV_REQUEST.ID_SERVICE_TYPE,
  user1229049.SERV_REQUEST.ID_SUB_SERVICE_TYPE,
  user1229049.SERV_REQUEST.ID_START_STATUS,
  user1229049.SERV_REQUEST.ID_SERVICE_STATUS,
  user1229049.SERV_REQUEST.ID_MODIFIED_LAST_BY,
  user1229049.SERV_REQUEST.INVENTORY_TAG,
  user1229049.SERV_REQUEST.REQUEST_POSTED_DATE,
  user1229049.SERV_REQUEST.REQUEST_REQUESTED_DATE,
  user1229049.SERV_REQUEST.REQUEST_OPEN_DATE,
  user1229049.SERV_REQUEST.REQUEST_MODIFIED_DATE,
  user1229049.SERV_REQUEST.REQUEST_CLOSED_DATE,
  user1229049.SERV_REQUEST.REQUEST_SERVICE_TIME_TOTAL,
  user1229049.SERV_REQUEST.ISSUE_DESCRIPTION,
  user1229049.SERV_NOTE.ID_NOTE,
  user1229049.SERV_NOTE.ID_SERV_REQUEST,
  user1229049.SERV_NOTE.ID_USER_TYPE,
  user1229049.SERV_NOTE.POSTED_DATE,
  user1229049.SERV_NOTE.NOTE
FROM
  user1229049.SYS_USER
  INNER JOIN user1229049.SERV_REQUEST ON (user1229049.SYS_USER.ID_USER = user1229049.SERV_REQUEST.ID_USER)
  INNER JOIN user1229049.SERV_NOTE ON (user1229049.SERV_REQUEST.ID_REQUEST = user1229049.SERV_NOTE.ID_SERV_REQUEST)

Here is the start of the trigger...

CREATE TRIGGER [SYS_USER (user1229049)].[SYS_USER_trd] ON [SYS_USER (user1229049)].[]
FOR DELETE
AS
BEGIN
  /* Trigger body */
END
GO

Any help would be great!

Az
0
Comment
Question by:Azurden
[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
5 Comments
 
LVL 11

Accepted Solution

by:
Otana earned 84 total points
ID: 18857497
Try this:

CREATE TRIGGER [SYS_USER (user1229049)].[SYS_USER_trd] ON [SYS_USER (user1229049)].[]
FOR DELETE
AS
BEGIN
  DELETE FROM SERV_NOTE WHERE ID_SERV_REQUEST IN (SELECT ID_REQUEST FROM SERV_REQUEST WHERE USER = (SELECT ID_USER FROM deleted))
  DELETE FROM SERV_REQUEST WHERE ID_USER = (SELECT ID_USER FROM deleted)
END
GO
0
 
LVL 25

Assisted Solution

by:jogos
jogos earned 83 total points
ID: 18857739
Other way to do this
Make a foreign key from serv_request  to the user-table and make it 'cascading delete'.
=> when you delete the user automaticly all the refering records wil be deleted
0
 
LVL 8

Assisted Solution

by:doobdave
doobdave earned 83 total points
ID: 18857773
Hi there,

may I recommend to not use triggers for this purpose?
You may or may not be aware that it's possible to have a CASCADE delete between related tables.
This is specified when creating the Foreign Key constraint:

ALTER TABLE tablename ADD CONSTRAINT foreignkeyname FOREIGN KEY(referencingcolumn) REFERENCES referencedtablename(referencedcolumnname) ON DELETE CASCADE

Best Regards,

David
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

630 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