﻿<?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 2008 / T-SQL (SS2K8)  / working with union all or union / 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>Fri, 24 May 2013 02:52:00 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: working with union all or union</title><link>http://www.sqlservercentral.com/Forums/Topic640674-392-1.aspx</link><description>[quote][b]lucassouzace (1/22/2009)[/b][hr]ok, but this example, the query using union all more better union do you think ?[/quote]In your specific example, because there are no overlaps (rows in more than one of the resultsets) union all is better as it won't require a distinct sort. In general, it depends on whether or not there's a chance of overlapping rows and, if there is a chance, if you want the duplicates removing or not.</description><pubDate>Thu, 22 Jan 2009 05:42:58 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: working with union all or union</title><link>http://www.sqlservercentral.com/Forums/Topic640674-392-1.aspx</link><description>ok, but this example, the query using union all more better union do you think ?</description><pubDate>Thu, 22 Jan 2009 05:28:44 GMT</pubDate><dc:creator>lucassouzace</dc:creator></item><item><title>RE: working with union all or union</title><link>http://www.sqlservercentral.com/Forums/Topic640674-392-1.aspx</link><description>Here is a simple illustration.  Run the following:[font="Courier New"]SELECT 1,'My Test 1'UNION SELECT 1,'My Test 1'UNION SELECT 2,'My Test 2'SELECT 1,'My Test 1'UNION ALL SELECT 1,'My Test 1'UNION ALL SELECT 2,'My Test 2'[/font]You will notice that the UNION groups by all fields and the UNION ALL does not (it will return a duplicate row).  So, UNION needs to collect everything and group it together to remove the duplicates.  Because of this it will always have a higher cost.</description><pubDate>Wed, 21 Jan 2009 07:37:32 GMT</pubDate><dc:creator>Michael Earl-395764</dc:creator></item><item><title>working with union all or union</title><link>http://www.sqlservercentral.com/Forums/Topic640674-392-1.aspx</link><description>hi people, suposse i've query:CREATE TABLE [dbo].[MyTest3]( [Id] [int] NULL, [Fname] [varchar](100) NULL, [Lname] [varchar](100) NULL, [salary] [money] NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOafterinsert into MyTest3 select 1 , 'John' , 'Smith' , 150000.00union select 2 , 'Hillary' , 'Swank' , 250000.00union select 3 , 'Elisa' , 'Smith' , 120000.00union select 4 , 'Liz' , 'Carleno' , 151000.00union select 5 , 'Tony' , 'Mcnamara' , 150300.00What´s difference between This code: ??insert into MyTest3 select 1 , 'John' , 'Smith' , 150000.00union ALL select 2 , 'Hillary' , 'Swank' , 250000.00union ALL select 3 , 'Elisa' , 'Smith' , 120000.00union ALL select 4 , 'Liz' , 'Carleno' , 151000.00union ALL select 5 , 'Tony' , 'Mcnamara' , 150300.00i'm lookup Plan Execution, using only 'union' i'm think then has more cost... and union all is less.</description><pubDate>Wed, 21 Jan 2009 07:02:05 GMT</pubDate><dc:creator>lucassouzace</dc:creator></item></channel></rss>