Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
Conditional Delete not working
Conditional Delete not working
Rate Topic
Display Mode
Topic Options
Author
Message
dwilliscp
dwilliscp
Posted Wednesday, November 14, 2012 9:00 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
Why does this delete all my records, reguardless of company?
delete Plant_Master
where EXISTS(Select Plant_CD from Plant_Master where company like 'MSC')
Post #1384698
Eugene Elutin
Eugene Elutin
Posted Wednesday, November 14, 2012 9:09 AM
SSCrazy
Group: General Forum Members
Last Login: Yesterday @ 11:22 AM
Points: 2,541,
Visits: 4,370
dwilliscp (11/14/2012)
Why does this delete all my records, reguardless of company?
delete Plant_Master
where EXISTS(Select Plant_CD from Plant_Master where company like 'MSC')
does at least one row exist in Plant_Master where company = 'MSC'?
If so then EXISTS(Select Plant_CD from Plant_Master where company like 'MSC') evaluates to true and all records from Plant_Master are deleted.
If you want to delete records from Plant_Master where company = 'MCS' then you need to do :
delete Plant_Master
where company = 'MSC'
If you want to delete records from Plant_Master where company strats from 'MCS' then you need to do :
delete Plant_Master
where company LIKE 'MSC%'
If you want to delete records from Plant_Master where company contains 'MCS' then you need to do :
delete Plant_Master
where company LIKE '%MSC%'
_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
Post #1384707
dwilliscp
dwilliscp
Posted Wednesday, November 14, 2012 12:25 PM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
Hmm thought it would do a line by line check.. turns out that my thought process was off anyway. Given that we have data here that is not in SAP, I can not do any conditional deletes. I will have to use the update statement to update existing records, instead of deleting and re-loading.
However if the plan had not changed.. then the delete statement would still not have worked quite right. I still wonder how that would have worked. The idea was to have it select all records that existed and refresh only those... to get the list you would have to run the following SQL statement:
Select plant.Plant
from file_Plants as plant
left outer join Plant_Master as mas
on mas.Plant_CD = plant.Plant
where NOT(mas.Plant_CD is NULL)
Post #1384809
Sean Lange
Sean Lange
Posted Wednesday, November 14, 2012 12:37 PM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 10:26 PM
Points: 8,606,
Visits: 8,247
dwilliscp (11/14/2012)
Hmm thought it would do a line by line check.. turns out that my thought process was off anyway. Given that we have data here that is not in SAP, I can not do any conditional deletes. I will have to use the update statement to update existing records, instead of deleting and re-loading.
However if the plan had not changed.. then the delete statement would still not have worked quite right. I still wonder how that would have worked. The idea was to have it select all records that existed and refresh only those... to get the list you would have to run the following SQL statement:
Select plant.Plant
from file_Plants as plant
left outer join Plant_Master as mas
on mas.Plant_CD = plant.Plant
where NOT(mas.Plant_CD is NULL)
Just my personal preference but I find this type of check to be far easier to read with a positive test.
where mas.Plant_CD IS NOT NULL
Of course in the case of this specific query why not just change the left join to an inner join since that is the result set you want to find.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1384813
dwilliscp
dwilliscp
Posted Wednesday, November 14, 2012 12:59 PM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
True, just a personal quirk. I find that when I am trying to think through something... I tend to code the thought process. Thus the outer join filtering where not NULL.
Sean Lange (11/14/2012)
dwilliscp (11/14/2012)
Hmm thought it would do a line by line check.. turns out that my thought process was off anyway. Given that we have data here that is not in SAP, I can not do any conditional deletes. I will have to use the update statement to update existing records, instead of deleting and re-loading.
However if the plan had not changed.. then the delete statement would still not have worked quite right. I still wonder how that would have worked. The idea was to have it select all records that existed and refresh only those... to get the list you would have to run the following SQL statement:
Select plant.Plant
from file_Plants as plant
left outer join Plant_Master as mas
on mas.Plant_CD = plant.Plant
where NOT(mas.Plant_CD is NULL)
Just my personal preference but I find this type of check to be far easier to read with a positive test.
where mas.Plant_CD IS NOT NULL
Of course in the case of this specific query why not just change the left join to an inner join since that is the result set you want to find.
Post #1384825
Eugene Elutin
Eugene Elutin
Posted Wednesday, November 14, 2012 3:46 PM
SSCrazy
Group: General Forum Members
Last Login: Yesterday @ 11:22 AM
Points: 2,541,
Visits: 4,370
dwilliscp (11/14/2012)
True, just a personal quirk. I find that when I am trying to think through something... I tend to code the thought process. Thus the outer join filtering where not NULL.
Sean Lange (11/14/2012)
dwilliscp (11/14/2012)
Hmm thought it would do a line by line check.. turns out that my thought process was off anyway. Given that we have data here that is not in SAP, I can not do any conditional deletes. I will have to use the update statement to update existing records, instead of deleting and re-loading.
However if the plan had not changed.. then the delete statement would still not have worked quite right. I still wonder how that would have worked. The idea was to have it select all records that existed and refresh only those... to get the list you would have to run the following SQL statement:
Select plant.Plant
from file_Plants as plant
left outer join Plant_Master as mas
on mas.Plant_CD = plant.Plant
where NOT(mas.Plant_CD is NULL)
Just my personal preference but I find this type of check to be far easier to read with a positive test.
where mas.Plant_CD IS NOT NULL
Of course in the case of this specific query why not just change the left join to an inner join since that is the result set you want to find.
I wouldn't call it "personal preference". It's wrong type of join, which can lead to worse performance.
That is exactly what INNER JOIN is for, so better to be written as
Select plant.Plant
from file_Plants as plant
inner join Plant_Master as mas
on mas.Plant_CD = plant.Plant
Now, as you select nothing from Plant_Master, you may find that the following will perform even better than INNER JOIN:
Select plant.Plant
from file_Plants as plant
where exists (select 1 from Plant_Master as mas
where mas.Plant_CD = plant.Plant)
Above two samples, can be actually called as "personal preference", but again, you may find that EXIST will give slightly better performance, as query processor will know that you need nothing from Plant_Master table.
_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
Post #1384910
dwilliscp
dwilliscp
Posted Thursday, November 15, 2012 8:17 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
Ok.. but how do you put that select logic into a delete statement?
Post #1385190
Sean Lange
Sean Lange
Posted Thursday, November 15, 2012 8:36 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 10:26 PM
Points: 8,606,
Visits: 8,247
dwilliscp (11/15/2012)
Ok.. but how do you put that select logic into a delete statement?
Change the word "select" to "delete".
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1385202
Sean Lange
Sean Lange
Posted Thursday, November 15, 2012 8:40 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 10:26 PM
Points: 8,606,
Visits: 8,247
Eugene Elutin (11/14/2012)
I wouldn't call it "personal preference". It's wrong type of join, which can lead to worse performance.
That is exactly what INNER JOIN is for, so better to be written as
My preference I meant was to conditional check, I did point out that it should be an inner join.
The personal preference was to use
where mas.Plant_CD is NOT NULL
instead of
where NOT(mas.Plant_CD is NULL)
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1385206
dwilliscp
dwilliscp
Posted Thursday, November 15, 2012 1:12 PM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
Ok, thanks for the help
Post #1385332
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.