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

MS SQL Trigger

Posted on 2007-04-05
5
625 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
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

766 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