Groups | Blog | Home
all groups > sql server programming > january 2005 >

sql server programming : Deleting across tables


-sakkie69-
1/8/2005 10:24:58 PM
Hi there,

If I have to delete from a table which has FK's from other tables, I just
want to confirm the syntax to
delete the table from the "sub" tables first before I can delete from the
main table

ie

I want to delete customers, but they have FK's on the table from orders (who
has an FK on it from shipped) so would this work -

delete a from
tb_shipped a
join tb_orders b on (a.orderid=b.orderid)
join tb_customers c on (a.custid and c.custid)
where custid in (1,2,3)

and then when that's done

delete a from
tb_orders a
join tb_customers b on (a.custid=b.custid)
where custid in (1,2,3)

I know that I can just do a simple delete from the orders where the custid
is in the value, but I am trying to get
to grips with these delete statements so please humour me :)

Thanks


Erland Sommarskog
1/8/2005 11:33:54 PM
-sakkie69- (clintonvb@gmail.com) writes:
[quoted text, click to view]

That "and" there is illegal.

The "custid" in the WHERE clause is ambiguous; you need to specify
an alias, to tell which table you mean.

And, if I may guess, the condition on custid should have b.custid,
not a.custid. Ir doesn't seem that tb_shipped should have a custid
column.

But all this is probably easier to run in Query Analyzer than asking
in a newsgroup.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
-sakkie69-
1/9/2005 6:13:27 PM
Cool thanks, the "And" was accidental as I was just typing this on the fly
:)

I just wanted to confirm that the syntax was ok and I was only going to
delete the right amount of data.

Cheers.



[quoted text, click to view]

AddThis Social Bookmark Button