SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 delete statement
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mikejohnson
Posting Yak Master

153 Posts

Posted - 12/04/2002 :  12:18:12  Show Profile  Visit mikejohnson's Homepage  Reply with Quote
Having problems writing a delete statement. Here is what I have so far and it doesn't work. I'm assuming I have something wrong, what is it and how do I accomplish what I want? If you can't tell by looking at the statement right now just let me know..

Thanks in advance, here's my statement:

delete FROM Student S
JOIN Purpose P on P.StudentID=S.StudentID
join TestResults T on T.StudentID=S.StudentID
WHERE S.ExamineeID=1062971

nr
SQL Server MVP & SQLTeam MVY

United Kingdom
12531 Posts

Posted - 12/04/2002 :  12:32:23  Show Profile  Visit nr's Homepage  Reply with Quote
This will delete all records in Student with ExamineeID=1062971
but only if there are corresponding entries in Purpose and TestResults.
If you want to delete the entries from all tables then define a cascade delete or use a transaction around the individual deletes.

begin tran
delete Purpose from Purpose P, Student S where P.StudentID=S.StudentID and S.ExamineeID=1062971
delete TestResults from TestResults T, Student S where T.StudentID=S.StudentID and S.ExamineeID=1062971
delete Student where ExamineeID=1062971
commit tran

add your own error processing to this.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
28518 Posts

Posted - 12/04/2002 :  15:41:42  Show Profile  Visit tkizer's Homepage  Reply with Quote
Shouldn't the statement be:

delete Student
FROM Student S
JOIN Purpose P on P.StudentID=S.StudentID
join TestResults T on T.StudentID=S.StudentID
WHERE S.ExamineeID=1062971

I believe that you need to list the table twice when doing a delete.


Go to Top of Page

ValterBorges
Flowing Fount of Yak Knowledge

USA
1429 Posts

Posted - 12/04/2002 :  16:03:17  Show Profile  Reply with Quote
FROM BOL:

This example shows the Transact-SQL extension used to delete records from a base table that is based on a join or correlated subquery. The first DELETE shows the SQL-92-compatible subquery solution, and the second DELETE shows the Transact-SQL extension. Both queries remove rows from the titleauthors table based on the titles stored in the titles table.
/* SQL-92-Standard subquery */
USE pubs
DELETE FROM titleauthor
WHERE title_id IN
(SELECT title_id
FROM titles
WHERE title LIKE '%computers%')

/* Transact-SQL extension */
USE pubs
DELETE titleauthor
FROM titleauthor INNER JOIN titles
ON titleauthor.title_id = titles.title_id
WHERE titles.title LIKE '%computers%



Edited by - ValterBorges on 12/04/2002 16:04:17
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
28518 Posts

Posted - 12/04/2002 :  16:26:55  Show Profile  Visit tkizer's Homepage  Reply with Quote
I should have said in my last post:

I believe that you need to list the table twice when doing a delete with a join.

In your example that would be the t-sql extension.


Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000 Version 3.4.03