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
 General SQL Server Forums
 New to SQL Server Programming
 New to SQL - need to bounce a script off some
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Sparky_
Starting Member

6 Posts

Posted - 04/12/2012 :  15:48:44  Show Profile  Reply with Quote
Greetings,

I have run into the need to delete some data out of my Sequel database at work.

I am not a SQL programmer. I have been going through some tutorials and the "dummies" book.

Because deleting from our main data is involved, before I pull the trigger, can I post the script and ask for your input?

I would hate to mess up for lack of asking for some input.

I have 2 scripts, I "think" both may delete the same way.

Thanks
-Sparky_

tkizer
Almighty SQL Goddess

USA
35020 Posts

Posted - 04/12/2012 :  16:16:11  Show Profile  Visit tkizer's Homepage  Reply with Quote
Please post it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sparky_
Starting Member

6 Posts

Posted - 04/12/2012 :  16:33:01  Show Profile  Reply with Quote
First – pardon my lingo regarding Sequel – I may call something incorrectly

I have a program that feeds our Sequel database.

Within each line of data that I want deleted I was able to set a variable called, “Delete_This” to 1 through the program that feeds Sequel.

So I believe bottom line everything I want deleted has Delete_This = 1.

However, as a safety check every piece of data that I want deleted from our MAIN tables should also exist in a received table.

What I want to do is delete (from our 12 tables within our Main database) all the lines that have Delete_This = 1 AND the data also exists in “received_table”

I have an ID field for every piece of data.

Here are my 2 scripts attempting to do the above description.

Select * from [Main_Tables].[dbo].[_Table1] Table1 where Exists (Select *
FROM [Received_Table].[dbo].[_Table1] ReceivedTable1
Where Table1.Delete_This = 1 AND ReceivedTable1.ID = Table1. ID
);


Select * from [Main_Tables].[dbo].[_Table1] Table1 where Table1.Delete_This = 1 AND Exists (Select *
FROM [Received_Table].[dbo].[_Table1] ReceivedTable1
where ReceivedTable1. ID = Table1.WMGUID
);


I would repeat this for Tables 1-12

Again – I want to delete from Main_Tables, those lines that have “Delete_This = 1” AND
The same ID exists in both Main Tables and Received Table.

The data was imported from Received_Tables INTO Main and there was a problem – I want to delete those and get back to pre-import.

Thanks for the help.

-Sparky


Go to Top of Page

Sparky_
Starting Member

6 Posts

Posted - 04/12/2012 :  16:44:44  Show Profile  Reply with Quote
Oh -
and I realize the above do not "delete" - my intention is to replace "Select" with "Delete" if you agree with my script.

The above scripts - just selecting - seem to give the correct counts -

I want to delete from "Main" only and then import again from "received"

Thanks again
Sparky_

Go to Top of Page

tkizer
Almighty SQL Goddess

USA
35020 Posts

Posted - 04/12/2012 :  17:23:25  Show Profile  Visit tkizer's Homepage  Reply with Quote
I'm thinking you want this:

Select * from [Main_Tables].[dbo].[_Table1] Table1
where Exists (Select * FROM [Received_Table].[dbo].[_Table1] ReceivedTable1 Where ReceivedTable1.ID = Table1. ID)
and Table1.Delete_This = 1

I've moved the Delete_This part outside of the exists.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sparky_
Starting Member

6 Posts

Posted - 04/12/2012 :  17:33:13  Show Profile  Reply with Quote
And am I correct that I simply change the word Select to Delete?

Thanks so much!!
Sparky_
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
35020 Posts

Posted - 04/12/2012 :  17:49:24  Show Profile  Visit tkizer's Homepage  Reply with Quote
delete Table1
from [Main_Tables].[dbo].[_Table1] Table1
where Exists (Select * FROM [Received_Table].[dbo].[_Table1] ReceivedTable1 Where ReceivedTable1.ID = Table1. ID)
and Table1.Delete_This = 1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sparky_
Starting Member

6 Posts

Posted - 04/13/2012 :  08:22:29  Show Profile  Reply with Quote
Thanks!!
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
35020 Posts

Posted - 04/13/2012 :  12:04:05  Show Profile  Visit tkizer's Homepage  Reply with Quote
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
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.09 seconds. Powered By: Snitz Forums 2000