Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2005, 03:36 PM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Detele Statement

How can I combine these quries into one SQL query?
I want to be able to delete from multiple table in one statement...

DELETE FROM COMPANYS
WHERE company_id <> 5 and company_id <> 41 and company_id <> 20;

DELETE FROM DIVISIONS
WHERE company_id <> 5 and company_id <> 41 and company_id <> 20;

DELETE FROM PLANS
WHERE company_id <> 5 and company_id <> 41 and company_id <> 20;


 
Old September 14th, 2005, 03:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can't.

By definition, the DELETE statement deletes row from a single table only.

Why do you think you need to do this in a single statement?

Wrap your multiple statements in a transaction if you need to be sure they all complete atomically, or are all rolled back if they don't.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 14th, 2005, 04:07 PM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff,
What do you mean by wrapping multiple statements in a transaction?
Could you please explain?

Thank you!


Quote:
quote:Originally posted by Jeff Mason
 You can't.

By definition, the DELETE statement deletes row from a single table only.

Why do you think you need to do this in a single statement?

Wrap your multiple statements in a transaction if you need to be sure they all complete atomically, or are all rolled back if they don't.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 15th, 2005, 06:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Managing transactions is generally easier to do in the host language, IMO. There, you would define a transaction on the connection to the database, begin the transaction, execute the stored procedure that contains your delete statements (or execute them individually), then on success commit the transaction or on an exception rollback the transaction, undoing all effects of the SQL statements you've executed.

This is the SQL Language forum; while the basic transaction mechanism is part of the SQL Standard, the implementation details (especially error handling and flow control) may differ between particular products.

You define and begin a transaction with the BEGIN TRANSACTION statement, commit the transaction with the COMMIT TRANSACTION statement, and rollback the transaction with ROLLBACK TRANSACTION.

The awkward part comes in detecting whether to do a COMMIT or ROLLBACK. In SQL Server's T/SQL language, the @@ERROR variable can be tested after each statement. If the value is nonzero, an error has occurred so you ROLLBACK the transaction. If all statements succeed, you COMMIT instead.

There is a whole other issue regarding the locking of data while the delete is being executed. Not only does a transaction allow you to declare a set of UPDATE/DELETE statements that should execute atomically, you can also control how others may, or may not, view the data while you are in the process of changing it, or whether or not you "see" any changes that they may be making.

That is called the transaction isolation level and that is a subject for another post.

Code to manage transactions (in T/SQL) can be structured like this:
Code:
DECLARE @GotError integer
SET @GotError = 0

BEGIN TRANSACTION

DELETE FROM <yourtable> ...
IF @@ERROR > 0 SET @GotError = @@ERROR

IF @GotError = 0 BEGIN
    DELETE FROM <yourtable> ...
    IF @@ERROR > 0 SET @GotError = @@ERROR
END

IF @GotError = 0 BEGIN
    DELETE FROM <yourtable> ...
    IF @@ERROR > 0 SET @GotError = @@ERROR
END
...

If @GotError = 0
    COMMIT TRANSACTION
else
    ROLLBACK TRANSACTION
Like I said, simpler in the host...




Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old October 12th, 2005, 12:31 AM
Registered User
 
Join Date: Aug 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

DELETE FROM COMPANYS C1,DIVISIONS D1,PLANS P1
WHERE c1.company_id=d1.company_id=p1.company_id
AND company_id <> 5 and company_id <> 41 and company_id <> 20;

It is maybe right. you can try i think the PL/SQL is not good way. because it is very slow.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with SQL Statement forumsUID SQL Server 2000 1 August 23rd, 2007 08:08 AM
convert a SQL Statement from MS Access to a SQL Corey Access 6 March 28th, 2007 12:33 PM
SQL Statement??? carswelljr SQL Server 2000 2 August 25th, 2006 12:40 PM
SQL Statement help johnman Access 1 October 22nd, 2004 10:20 AM
sql statement [email protected] Beginning VB 6 2 June 23rd, 2003 04:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.