Solved

MS SQL Trigger

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
t-sql, insert data in table normally, but read from view 3 30
SSIS GUID Variable 2 30
Need sql in string 2 28
Trigger C# code inside the SQL Server 6 25
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

685 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