Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Running a simple Negative query

Author  Topic 

hasanali00
Posting Yak Master

207 Posts

Posted - 2005-08-11 : 10:14:27
I am trying to run a simple query. I want to delete all the products where the company ID is NOT 1 or 2.

DELETE FROM Viamed_Products
WHERE companyid != 1 OR != 2

But I am getting errors. What could be the solution?
thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-11 : 10:17:48
DELETE FROM Viamed_Products
WHERE companyid not in (1,2)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

hasanali00
Posting Yak Master

207 Posts

Posted - 2005-08-11 : 10:26:34
Madhivanan: What have u made me do.
Your query deleted everything that had companyid 1 & 2.

I wanted to delete everything BUT products belonging to companyid1 & 2.

How can I get all the data back???
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-11 : 10:46:17
>>Your query deleted everything that had companyid 1 & 2.

No it wont. I used not in (1,2)

>>How can I get all the data back???

If you have back up restore it

First test all your queries at Test database and then run them at Production

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-08-11 : 10:47:44
"Madhivanan: What have u made me do.
Your query deleted everything that had companyid 1 & 2.
I wanted to delete everything BUT products belonging to companyid1 & 2.
How can I get all the data back???"

1. you got free advice....all such advice is given on a use-it-at-your-peril basis.
2. "DELETE FROM Viamed_Products WHERE companyid not in (1,2)"...should have worked UNLESS you LEFT OUT the NOT part???
3. good advice....for future datebase update statements (del,upd, etc)...start with BEGIN TRANSACTION....and only after you are happy with results, issue a COMMIT TRANSACTION
4. without knowing the full picture...ie table DDL, etc...blame cannot be fired this way in any serious manner.

5. to recover go to your database backup!...that's what's it's for.
Go to Top of Page

hasanali00
Posting Yak Master

207 Posts

Posted - 2005-08-11 : 11:17:21
thanks goodnes, everything is fine. I just paniced for no reason
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-12 : 00:48:26
quote:
Originally posted by hasanali00

thanks goodnes, everything is fine. I just paniced for no reason



As said it is always better to test the queries at Test Database and if you are satisfied with the results then run them in Production database

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -