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
 General SQL Server Forums
 New to SQL Server Programming
 New to SQL - need to bounce a script off some

Author  Topic 

Sparky_
Starting Member

6 Posts

Posted - 2012-04-12 : 15:48:44
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

38200 Posts

Posted - 2012-04-12 : 16:16:11
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 - 2012-04-12 : 16:33:01
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 - 2012-04-12 : 16:44:44
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

38200 Posts

Posted - 2012-04-12 : 17:23:25
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 - 2012-04-12 : 17:33:13
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

38200 Posts

Posted - 2012-04-12 : 17:49:24
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 - 2012-04-13 : 08:22:29
Thanks!!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-13 : 12:04:05
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
   

- Advertisement -