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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Union and Union All

Author  Topic 

johnsql
Posting Yak Master

161 Posts

Posted - 2007-10-01 : 09:25:55
I make a table variable and I'd like to insert to insert 6 rows into that table using SELECT statements. If I use UNION only, I get only 5 rows inserted. If I use UNION ALL, I get 6 rows inserted. So, please tell me why UNION does not work correctly?


declare @sample table (ID int, TIME_SPENT int, [DATE] datetime, DAY_LENGTH int, TOTAL_AUMONT money)

-- only 5 rows inserted if using UNION
insert @sample
select 11516, 60, '27-Sep-07', 510, 7.5
union select 11516, 15, '27-Sep-07', 510, 8.25
union select 11516, 30, '27-Sep-07', 510, 8
union select 11846, 30, '27-Sep-07', 480, 7.5
union select 11846, 15, '27-Sep-07', 480, 7.75
union select 11846, 30, '27-Sep-07', 480, 7.5

-- 6 rows inserted if using UNION ALL
declare @sample table (ID int, TIME_SPENT int, [DATE] datetime, DAY_LENGTH int, TOTAL_AUMONT money)

insert @sample
select 11516, 60, '27-Sep-07', 510, 7.5
union all select 11516, 15, '27-Sep-07', 510, 8.25
union all select 11516, 30, '27-Sep-07', 510, 8
union all select 11846, 30, '27-Sep-07', 480, 7.5
union all select 11846, 15, '27-Sep-07', 480, 7.75
union all select 11846, 30, '27-Sep-07', 480, 7.5

Thanks in advance.
johnsql

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2007-10-01 : 09:32:23
from BOL...

"
UNION
Specifies that multiple result sets are to be combined and returned as a single result set.

ALL
Incorporates all rows into the results. This includes duplicates. If not specified, duplicate rows are removed.

"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-03 : 02:49:17
http://www.fmsinc.com/free/NewTips/SQL/SQLtip5.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -