sql server - Why does my trigger not fire? -


i have following table:

if object_id(n'dbo.node') null create table dbo.node (     id bigint identity primary key,     parentid bigint null foreign key references node(id) on delete no action,     datecreated datetime not null,     lastupdated datetime not null,     [name] nvarchar(500) not null, ); 

now, because sql server complains when try set foreign key cascaded delete, have created trigger work:

create trigger node_delete on node delete begin     delete node parentid in (select id deleted) end 

now here's sample data set:

id                   parentid             datecreated             lastupdated             name 520                  1                    2010-01-12 02:26:26.890 2010-01-12 02:26:26.890 test 1 523                  520                  2010-01-12 02:32:44.777 2010-01-12 02:32:44.777 test 2 

now let's run bit of sql:

delete node id=520 

the node should delete, along child node. why error?

msg 547, level 16, state 0, line 1 delete statement conflicted same table reference constraint "fk__node__parentid__117f9d94". conflict occurred in database "mydb", table "dbo.node", column 'parentid'. statement has been terminated. 

the foreign key blocks initial delete, , trigger never fires. try instead of trigger deletes furthest decedent on up.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -