﻿<?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 7,2000 / SQL Server Newbies  / Update Script to select / 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 23:33:20 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>SQLSaturday#57http://dbace.us/repriser/images/abstractwhyquerysoslow_.html</description><pubDate>Sat, 14 Jan 2012 08:51:50 GMT</pubDate><dc:creator>jswong05</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>[quote][b]rodneykee (1/2/2012)[/b][hr]Terry,This is what I would've done:update ipadmin.GL_TXN set defunct = 'Y' from ipadmin.gl_txn where transacted_on &amp;gt;= convert(datetime, '03/07/2008', 103) and transacted_on &amp;lt; convert(datetime, '03/07/2008', 103) + 1 and belongs_to_hcare in (100)Rodney :-)[/quote]This solution assumes that every member of a set of gl_group_id matches the filter. The query as originally written would update all members of a set of gl_group_id, regardless of how many members matched. So, if there are 10 rows with gl_group_id = 3 and only 1 of these matches the filter, all 10 rows will be updated by OP's query. The query above would only update one row - as Max has pointed out.[code="sql"]update ipadmin.GL_TXN set defunct = 'Y' where gl_group_id in 	(select 		gl_group_id 	from ipadmin.gl_txn 	where transacted_on &amp;gt;= convert(datetime, '03/07/2008', 103) 		and transacted_on &amp;lt; convert(datetime, '03/07/2008', 103) + 1 		and belongs_to_hcare in (100) 	GROUP BY GL_GROUP_ID)[/code]- same as the original without the redundant DISTINCT. Run it in SSMS and check the actual plan, compare time against running through application. At this point you don't know if it's the query or the app. If the run times are the same, then please post the actual plan, along with ddl and dml for table ipadmin.GL_TXN.</description><pubDate>Mon, 02 Jan 2012 16:21:54 GMT</pubDate><dc:creator>ChrisM@home</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Terry,This is what I would've done:update ipadmin.GL_TXN set defunct = 'Y' from ipadmin.gl_txn where transacted_on &amp;gt;= convert(datetime, '03/07/2008', 103) and transacted_on &amp;lt; convert(datetime, '03/07/2008', 103) + 1 and belongs_to_hcare in (100)Rodney :-)</description><pubDate>Mon, 02 Jan 2012 16:00:01 GMT</pubDate><dc:creator>rodneykee</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Hi Terry,How you can tell that gl_group_id column is a unique key in ipadmin.GL_TXN table?I agree that your's Northwind example is working fine, but we know that orderid column is unique for orders table.I'm not sure if you got my point? You are making assumption, which may or may not be true.Cheers, Max</description><pubDate>Thu, 07 Aug 2008 15:35:16 GMT</pubDate><dc:creator>Maksymilian Mulawa</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>As I'm leaving for the day and don't have time to create a more robust example, try this on Northwind. Ten bucks says you get the same results...If you don't agree, I'll create another example for you tomorrow.select * from orders where OrderId in(select distinct orderid from orders where orderdate &amp;gt;convert(datetime, '07/03/1996', 103) 	and orderdate &amp;lt;= convert(datetime, '07/08/1996', 103) + 1)select * from orders where orderdate &amp;gt;convert(datetime, '07/03/1996', 103) and orderdate &amp;lt;= convert(datetime, '07/08/1996', 103) + 1</description><pubDate>Thu, 07 Aug 2008 14:50:57 GMT</pubDate><dc:creator>  tosscrosby</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Your query is not an equivalent to the original one, as the result of subquery is just list of 'gl_group_id' and then you are updating records with these gl_group_id, not records filter by transacted_on and belongs_to_hcare.</description><pubDate>Thu, 07 Aug 2008 14:28:56 GMT</pubDate><dc:creator>Maksymilian Mulawa</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Unless I am totally missing something, wouldn't this do what you're looking for??declare @lessdate = datetimedeclare @greaterdate = datetimeset @lessdate = convert(datetime, '03/07/2008', 103)set @greaterdate = convert(datetime, '03/07/2008', 103) + 1update ipadmin.GL_TXN  set defunct = 'Y' where transacted_on &amp;gt;= @lessdateand transacted_on &amp;lt; @greaterdateand belongs_to_hcare in (100)</description><pubDate>Thu, 07 Aug 2008 13:58:51 GMT</pubDate><dc:creator>  tosscrosby</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Ayie, You should wrap this T-SQL code into stored procedure, or if you cannot create stored procedures, by using SqlCommand method ExecuteNonQuery you can submit full batch with temporary table code etc.</description><pubDate>Thu, 07 Aug 2008 02:56:27 GMT</pubDate><dc:creator>Maksymilian Mulawa</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Hi Mulawa,This code is from VB,  so i cannot create temp table for this, can can you give me the script using in visual basic as well? thanks</description><pubDate>Thu, 07 Aug 2008 02:49:51 GMT</pubDate><dc:creator>Ayie</dc:creator></item><item><title>RE: Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Hi,You could consider putting the data from the subquery to temp table [code]Create table #temp(  gl_group_id Int )insert into #tempselect distinct gl_group_idfrom ipadmin.gl_txnwhere transacted_on &amp;gt;= convert(datetime, '03/07/2008', 103)and transacted_on &amp;lt; convert(datetime, '03/07/2008', 103) + 1and belongs_to_hcare in (100) [/code]--You don't need the GROUP BY, as it makes no sense in this context--If #temp has few hundred, thousands rows try creating index on gl_group_id column.[code]update ipa  set defunct = 'Y'from ipadmin.GL_TXN ipa inner join #temp t  ON ipa.gl_group_id = t.gl_group_id[/code]Also one of the optimization options would be to create clustered index on  ipadmin.gl_txn(transacted_on) column, this would speed up the datetime range query.</description><pubDate>Thu, 07 Aug 2008 02:16:23 GMT</pubDate><dc:creator>Maksymilian Mulawa</dc:creator></item><item><title>Update Script to select</title><link>http://www.sqlservercentral.com/Forums/Topic547965-169-1.aspx</link><description>Hello everyone, any can help me to this script i want to enhance this script into fastest way, because if i run this query its to long to process, i dont know why, the total records without distinct is only 2000 records, anyone can help me please thanks[quote]update ipadmin.GL_TXN  set defunct = 'Y' where gl_group_id in (select distinct gl_group_id from ipadmin.gl_txn with (nolock) where transacted_on &amp;gt;= convert(datetime, '03/07/2008', 103) and transacted_on &amp;lt; convert(datetime, '03/07/2008', 103) + 1 and belongs_to_hcare in (100) GROUP BY GL_GROUP_ID)[/quote]</description><pubDate>Wed, 06 Aug 2008 20:05:04 GMT</pubDate><dc:creator>Ayie</dc:creator></item></channel></rss>