473,378 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Create Delete Trigger on Table1 to Update a filed on Table2

Yas
Hi everyone

I am trying to create a DELETE Trigger. I have 2 tables. Table1 and
Table2. Table 2 has all the same fields and records as Table1 + 1
extra column "date_removed"

I would like that when a record is deleted from Table 1, the trigger
finds that record in Table2 and updates the date_removed filed with
current time stamp.
The primary key on both is combination of domain,admin_group and cn.
CREATE TRIGGER [tr_updateTable2] ON [dbo].tbl_Table1
FOR DELETE
AS
Update Table2
SET date_removed = getDate()

I'm stuck here, how do I manipulate on Table2 only the records that
were deleted on Table1, so to only update date_removed filed for them
in Table2?
I guess i need to compare domain, cn and admin_group, but I don't know
how.
Any help would be greatly appreciated

Thanks! :-)
Jan 11 '08 #1
3 3879
Yas wrote:
I am trying to create a DELETE Trigger. I have 2 tables. Table1 and
Table2. Table 2 has all the same fields and records as Table1 + 1
extra column "date_removed"

I would like that when a record is deleted from Table 1, the trigger
finds that record in Table2 and updates the date_removed filed with
current time stamp.
The primary key on both is combination of domain,admin_group and cn.
CREATE TRIGGER [tr_updateTable2] ON [dbo].tbl_Table1
FOR DELETE
AS
Update Table2
SET date_removed = getDate()

I'm stuck here, how do I manipulate on Table2 only the records that
were deleted on Table1, so to only update date_removed filed for them
in Table2?
I guess i need to compare domain, cn and admin_group, but I don't know
how.
update Table2
set date_removed = GetDate()
from Table2 t
join deleted d on t.domain = d.domain
and t.cn = d.cn
and t.admin_group = d.admin_group
Jan 11 '08 #2
On Fri, 11 Jan 2008 06:49:02 -0800 (PST), Yas wrote:
>Hi everyone

I am trying to create a DELETE Trigger. I have 2 tables. Table1 and
Table2. Table 2 has all the same fields and records as Table1 + 1
extra column "date_removed"

I would like that when a record is deleted from Table 1, the trigger
finds that record in Table2 and updates the date_removed filed with
current time stamp.
The primary key on both is combination of domain,admin_group and cn.
CREATE TRIGGER [tr_updateTable2] ON [dbo].tbl_Table1
FOR DELETE
AS
Update Table2
SET date_removed = getDate()

I'm stuck here, how do I manipulate on Table2 only the records that
were deleted on Table1, so to only update date_removed filed for them
in Table2?
I guess i need to compare domain, cn and admin_group, but I don't know
how.
Any help would be greatly appreciated

Thanks! :-)
Hi Yas,

Here's an alternative, using a more portable syntax for the UPDATE
statement, that also has less "issues" (but don't worry - none of these
issues affect you when joining on the primary key, as is the case here;
I just wanted to provide this alternative for completeness' sake).

UPDATE Table2
SET DateRemoved = CURRENT_TIMESTAMP
WHERE EXISTS
(SELECT *
FROM deleted AS d
WHERE d.domain = Table2.domain
AND d.cn = Table2.cn
AND d.admin_group = Table2.admin_group);

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Jan 11 '08 #3
Yas (ya****@gmail.com) writes:
I would like that when a record is deleted from Table 1, the trigger
finds that record in Table2 and updates the date_removed filed with
current time stamp.
The primary key on both is combination of domain,admin_group and cn.
CREATE TRIGGER [tr_updateTable2] ON [dbo].tbl_Table1
FOR DELETE
AS
Update Table2
SET date_removed = getDate()

I'm stuck here, how do I manipulate on Table2 only the records that
were deleted on Table1, so to only update date_removed filed for them
in Table2?
I guess i need to compare domain, cn and admin_group, but I don't know
how.
Any help would be greatly appreciated
Ed and Hugo gave you the code, but they did not really explain what
"deleted" is. In case you don't know it: in a trigger, you have access
to two virtual tables, "inserted" and "deleted". "inserted" holds the
after-image of the affected rows for an INSERT and UPDATE statement.
"deleted" holds a before-image of the affected rows for an UPDATE and
DELETE statement. "inserted" is empty with DELETE and "deleted" is
empty with INSERT.

Note that they are only visible directly in a trigger, and you cannot
access them from a stored procedure or dynamic SQL invoked by a trigger.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 11 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gary MacKay | last post by:
I have a typical setup with a master table that has a related table with many records per master record. Since MySql does not support sub-selects, how do I delete just the set of records I want? ...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: Mitchell Thomas | last post by:
I have 3 tables and they are all a one-one relationship. The tables are in oracle with Access as the front end. If user deletes a record from Table1, how do I programmatically delete the...
14
by: Karl O. Pinc | last post by:
Hi, Thought perhaps some other eyes than mine can tell if I'm doing something wrong here or if there's a bug somewhere. I've never passed a ROWTYPE varaible to a function but I don't see where...
3
by: Mahesh S | last post by:
Hi I have 3 tables in db2 database. I would like to write a cascading delete statement and I dont want to use a trigger. Could you please explain how the statement needs to be? My table...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
0
by: Shootah | last post by:
Hi, I have succeeded in adding automated relationships with refference tables after importing an excel file created from a query to an Access database. However I have the following problem: ...
2
by: Yas | last post by:
Hello All, I have 2 tables in a MS SQL DB. Table1 and Table2.Log Table2.Log was a copy of Table1 + an extra column for date_deleted. I have 2 Triggers on Table1, Insert and delete. So when a...
1
by: garymarshallsa | last post by:
I am new to SQL and have a MSSQL 2000 database data_02 that consists of two tables Table1 oeordhdr_sql fields ORDDATE Table2 oeordlin_sql fields REQDATE, SHPDATE I am needing a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.