﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Conditional Delete not working / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 07:55:16 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>Ok, thanks for the help :-D</description><pubDate>Thu, 15 Nov 2012 13:12:24 GMT</pubDate><dc:creator>dwilliscp</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>[quote][b]Eugene Elutin (11/14/2012)[/b][hr]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  [/quote]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 [code]where mas.Plant_CD is NOT NULL[/code]instead of [code]where NOT(mas.Plant_CD is NULL)[/code]</description><pubDate>Thu, 15 Nov 2012 08:40:50 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>[quote][b]dwilliscp (11/15/2012)[/b][hr]Ok.. but how do you put that select logic into a delete statement?[/quote]Change the word "select" to "delete".</description><pubDate>Thu, 15 Nov 2012 08:36:31 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>Ok.. but how do you put that select logic into a delete statement?</description><pubDate>Thu, 15 Nov 2012 08:17:47 GMT</pubDate><dc:creator>dwilliscp</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>[quote][b]dwilliscp (11/14/2012)[/b][hr]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.[quote][b]Sean Lange (11/14/2012)[/b][hr][quote][b]dwilliscp (11/14/2012)[/b][hr]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)[/quote]Just my personal preference but I find this type of check to be far easier to read with a positive test.[code]where mas.Plant_CD IS NOT NULL[/code]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. :cool:[/quote][/quote]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.PlantNow, 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.</description><pubDate>Wed, 14 Nov 2012 15:46:14 GMT</pubDate><dc:creator>Eugene Elutin</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>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.[quote][b]Sean Lange (11/14/2012)[/b][hr][quote][b]dwilliscp (11/14/2012)[/b][hr]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)[/quote]Just my personal preference but I find this type of check to be far easier to read with a positive test.[code]where mas.Plant_CD IS NOT NULL[/code]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. :cool:[/quote]</description><pubDate>Wed, 14 Nov 2012 12:59:19 GMT</pubDate><dc:creator>dwilliscp</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>[quote][b]dwilliscp (11/14/2012)[/b][hr]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)[/quote]Just my personal preference but I find this type of check to be far easier to read with a positive test.[code]where mas.Plant_CD IS NOT NULL[/code]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. :cool:</description><pubDate>Wed, 14 Nov 2012 12:37:21 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>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)</description><pubDate>Wed, 14 Nov 2012 12:25:44 GMT</pubDate><dc:creator>dwilliscp</dc:creator></item><item><title>RE: Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>[quote][b]dwilliscp (11/14/2012)[/b][hr]Why does this delete all my records, reguardless of company?delete Plant_Masterwhere EXISTS(Select Plant_CD from Plant_Master where company like 'MSC')[/quote]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_Masterwhere company = 'MSC'If you want to delete records from Plant_Master where company strats from 'MCS' then you need to do :delete Plant_Masterwhere company LIKE 'MSC%'If you want to delete records from Plant_Master where company contains 'MCS' then you need to do :delete Plant_Masterwhere company LIKE '%MSC%'</description><pubDate>Wed, 14 Nov 2012 09:09:05 GMT</pubDate><dc:creator>Eugene Elutin</dc:creator></item><item><title>Conditional Delete not working</title><link>http://www.sqlservercentral.com/Forums/Topic1384698-338-1.aspx</link><description>Why does this delete all my records, reguardless of company?delete Plant_Masterwhere EXISTS(Select Plant_CD from Plant_Master where company like 'MSC')</description><pubDate>Wed, 14 Nov 2012 09:00:21 GMT</pubDate><dc:creator>dwilliscp</dc:creator></item></channel></rss>