Sayfayı Yazdır | Pencereyi Kapat

SQL Server UNION, UNION ALL, EXCEPT, INTERSECT

Nereden Yazdırıldığı: Datakent
Kategori: Diğer bölümler
Forum Adı: Microsoft SQL Server
Forum Tanımlaması: Microsoft SQL Server ile ilgili soru / sorun ve paylaşım bölümü
URL: http://forum.datakent.com/forum_posts.asp?TID=2025
Tarih: 19.Nisan.2024 Saat 05:05


Konu: SQL Server UNION, UNION ALL, EXCEPT, INTERSECT
Mesajı Yazan: murat turan
Konu: SQL Server UNION, UNION ALL, EXCEPT, INTERSECT
Mesaj Tarihi: 10.Aralik.2010 Saat 20:04
UNION, UNION ALL, EXCEPT, INTERSECT
 
*** EXCEPT, INTERSECT    komutları SQL Server 2008 de kullanılabilir ! Diğer deyimler önceki sürümlerde kullanılabiliyordu.
 
-- sql server 2008 ile gelen yeni deyimler except ve intersect
-- geçici tabloları yapalım ve test kayıtları ekleyelim
CREATE TABLE #UnionTest1
(
idcol int IDENTITY,
col2 char(3),
);
CREATE TABLE #UnionTest2
(
idcol int IDENTITY,
col4 char(3),
);
INSERT INTO #UnionTest1
VALUES
('AAA'),
('BBB'),
('CCC');
INSERT INTO #UnionTest2
VALUES
('CCC'),
('DDD'),
('EEE');
-- örnekler --
-- her iki ablodaki benzersiz tüm kayıtları listeliyor. bir nevi distinct
select col2 from #UnionTest1
UNION
select col4 from #Uniontest2
--her iki tablodaki tüm kayıtları koşulsuz listeler
select col2 from #UnionTest1
union all
select col4 from #UnionTest2

--ilk tablodaki benzersiz kayıtları listeler. ve ikinci tablodan herhangi bir veri almaz
--ikinci tablo sadece kontrol için kullanılmış olur
select col2 from #uniontest1
except
select col4 from #uniontest2
--alternatif select
select col2 from #uniontest1 as t1
where not exists(select col4 from #uniontest2 where col4 = t1.col2)

--her iki tabloda ortak/benzer kayıtları listeler
select col2 from #uniontest1
intersect
select col4 from #uniontest2
--alernatif select
select col2 from #uniontest1 as t1
where exists(select col4 from #uniontest2 where col4 = t1.col2)


-------------
http://www.kasatakip.com - Kasa Takip  |  http://www.caritakip.com - Cari Takip  |  http://www.evraktakip.com - Evrak Takip  |  http://www.etasqlmobil.com - ETA SQL Mobil



Sayfayı Yazdır | Pencereyi Kapat