472,371 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

TRIGGER after UPDATE

Hi there!
I need to write a trigger that will check referential integrity of my
data. I have few FOREIGN KEY constraints but, as You probably konow, the
cannot be deferred (in the meaning of SQL 92 standard). So I decided to
add NOCHECK CONSTRAINT ALL to the modified table and then run a trigger
(after secon altertion of my table). But I cannot write a trigger for
ALTER. I found something on msdn, byt their example doesn't work. To
show what's my problem look at this example:

CREATE TABLE Indeksy(
id_indeksu INT CONSTRAINT indeksy_pkey PRIMARY KEY,
numer INT CONSTRAINT wymagany NOT NULL
CONSTRAINT unikatowy UNIQUE
);

CREATE TABLE Studenci(
id_studenta INT CONSTRAINT studenci_pkey PRIMARY KEY,
indeks INT CONSTRAINT indeks_studenta REFERENCES Indeksy(id_indeksu)
ON DELETE CASCADE
ON UPDATE CASCADE
--DEFERRABLE INITIALLY DEFERRED
CONSTRAINT tylko_raz UNIQUE,
nazwisko VARCHAR(255) CONSTRAINT nazwisko_wymagane NOT NULL
);

GO
CREATE TRIGGER ReferentialIntegrityTriggerForStudenci
ON DATABASE
AFTER ALTER
AS
BEGIN
DELETE FROM Studenci WHERE Studenci.Indeks NOT IN (SELECT id_indeksu
FROM Indeksy);
END

GO
ENABLE TRIGGER ReferentialIntegrityTriggerForStudenci ON Studenci;
GO

INSERT INTO Indeksy VALUES (1,1111111);
INSERT INTO Indeksy VALUES (2,1211111);
INSERT INTO Studenci VALUES (1,1, 'Kowalski');
INSERT INTO Studenci VALUES (2,2, 'Nowak');

--deferred

ALTER TABLE Studenci
NOCHECK CONSTRAINT ALL

INSERT INTO Studenci VALUES (3,5, 'Odraczacz');
INSERT INTO Studenci VALUES (4,130, 'Powolny');
INSERT INTO Studenci VALUES (5,4, 'Grabowski');

INSERT INTO Indeksy VALUES (3,1121111);
INSERT INTO Indeksy VALUES (4,1112111);
INSERT INTO Indeksy VALUES (5,1111211);

ALTER TABLE Studenci
CHECK CONSTRAINT ALL

When I run this script I get a message: Msg 156, Level 15, State 1,
Procedure ReferentialIntegrityTriggerForStudenci, Line 4
Incorrect syntax near the keyword 'ALTER'.

Without it INSERT INTO Studenci VALUES (4,130, 'Powolny'); inserts
invalid data that cannot be checked... Of course this is only an example.

Could you, please, write simillar, WORKING :-) trigger for me?

Thanx!
Wojciech
May 14 '07 #1
3 5889
P.S.
The script must be MS SQL Server 2005 compatibile if it cares :-)
May 14 '07 #2
Wojto (je***********@interia.pl) writes:
I need to write a trigger that will check referential integrity of my
data. I have few FOREIGN KEY constraints but, as You probably konow, the
cannot be deferred (in the meaning of SQL 92 standard). So I decided to
add NOCHECK CONSTRAINT ALL to the modified table and then run a trigger
(after secon altertion of my table). But I cannot write a trigger for
ALTER. I found something on msdn, byt their example doesn't work. To
show what's my problem look at this example:
I'm afraid that the idea does not seem workable to me. At least it would
be a serious kludge. While I can agree that commit-time constraints had
been nice, I would recommend you to find a solution within the rules.
CREATE TRIGGER ReferentialIntegrityTriggerForStudenci
ON DATABASE
AFTER ALTER
You would need AFTER ALTER_TABLE.
AS
BEGIN
DELETE FROM Studenci WHERE Studenci.Indeks NOT IN (SELECT id_indeksu
FROM Indeksy);
END
But you don't want this code to be run each time someone performs
ALTER TABLE, so you would have read examine the result of the
eventdata() function to see if the statement apply to your tables.

As I said, it would be an enormous kludge, I definitely recommend you
to look for a different solution for your actual problem.

--
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
May 14 '07 #3
Thanx for Your reply! It was really helpfull for me and I really
appreciate it! ALTER_TABLE works good. But in the matter of fact now I
see that this solution is really far from beeing perfec...

Regards,
Wojtek
May 14 '07 #4

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

Similar topics

8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
3
by: Mike | last post by:
I have never written a trigger before, but I have a couple of uses for them now so I am trying. I have a view that I created and I want to have this trigger, run anytime the view is run. I don't...
5
by: Neil Rutherford | last post by:
During testing of an application, i noticed a difference between SQL 2000 and SQL 7, both with identical config. In a nutshell: A table has a trigger for UPDATE and DELETE. When a column in the...
1
by: Gent | last post by:
am using FOR UPDATE triggers to audit a table that has 67 fields. My problem is that this slows down the system significantly. I have narrowed down the problem to the size (Lines of code) that need...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
2
by: Net Virtual Mailing Lists | last post by:
Hello, If I have a rule like this: CREATE OR REPLACE RULE sometable_update AS ON UPDATE TO table2 DO UPDATE cache SET updated_dt=NULL WHERE tablename='sometable'; CREATE OR REPLACE RULE...
3
by: teddysnips | last post by:
I need a trigger (well, I don't *need* one, but it would be optimal!) but I can't get it to work because it references ntext fields. Is there any alternative? I could write it in laborious code...
2
by: mob1012 via DBMonster.com | last post by:
Hi All, I wrote last week about a trigger problem I was having. I want a trigger to produce a unique id to be used as a primary key for my table. I used the advice I received, but the trigger is...
5
by: wpellett | last post by:
I can not get the SQL compiler to rewrite my SQL UPDATE statement to include columns being SET in a Stored Procedure being called from a BEFORE UPDATE trigger. Example: create table...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.