Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing a noob trigger...

Status
Not open for further replies.

Rodie

Programmer
Jun 27, 2004
132
FR
Hi all :)

I'm very noob in SQL. I work on a survey stuff.

I'd like to write a trigger that does:
when I remove a row of the result table, I get the session_id column of this row and check if there are still rows with this same session_id. If not, I remove this session_id of the session table.

Here is what I did so far:
Code:
CREATE TRIGGER delete_session
ON result
{
	after delete
	as
	declare @sid int
	set sid = (select session_id from deleted)
	if not exists ( sid )
 	begin
 		delete from session where session_id = sid 
	end
}
I got this error: Error 0. Syntax error or access violation.
Can someone help me??
Thanks a lot ;-)
 
This should do it
CREATE TRIGGER delete_session
ON result
FOR Delete
as
declare @sid int
select @sid = session_id from deleted
if not exists ( select * from result where session_id = @sid)
begin
delete from session where session_id = sid
end
go

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Thanks a lot Denis :)
I just added the @ that I forgot: @sid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top