Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Triggers

Author  Topic 

sholana
Starting Member

4 Posts

Posted - 2002-06-18 : 11:09:34
I am trying to write a trigger to test if the entered value is in another table and rollback if not.... so far I have this...
But the select statement is no right help!!!

CREATE TRIGGER Advertisement_Table_insert ON Advertisement
FOR INSERT, UPDATE
AS
IF Update( Author_id)
Begin
IF Not (Author_id is NULL)
BEGIN
IF NOT EXISTS(SELECT User_id FROM USERS WHERE User_id = Author_id) <<<<<
BEGIN
Raiserror('Author_id not in Users Table', 16, 1)
ROLLBACK TRANSACTION
END
END
END

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-06-18 : 11:17:53
quote:

But the select statement is no right help!!!



It's not the select statement that is incorrect; your "IF Not (Author_id is NULL)" is incorrect. A trigger fires on the statement, regardless of the # of rows affected. Your trigger will fire on updates of 0, 1, or more than 1 row(s), so you need to qualify which row you are talking about when you check if the author_id column is null.

You might also consider creating a foreign key relationship between users.user_id and advertisement.author_id.

setBasedIsTheTruepath
<O>

Edited by - setbasedisthetruepath on 06/18/2002 11:18:13
Go to Top of Page
   

- Advertisement -