Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet230/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   226   227   228   229   230   231   232   233   ...   443
Bog'liq
BookSQL

The APPLY Operator

The APPLY operator is a very powerful table operator. Like all table operators, this operator is used 

in the FROM clause of a query. The two supported types of APPLY operator are CROSS APPLY and 

OUTER APPLYCROSS APPLY implements only one logical query processing phase, whereas OUTER 

APPLY implements two.

note

  APPLY isn’t standard; the standard counterpart is called LATERAL, but the standard 

form wasn’t implemented in SQL Server.

The APPLY operator operates on two input tables, the second of which can be a table expression; 

I’ll refer to them as the “left” and “right” tables. The right table is usually a derived table or an inline 

TVF. The CROSS APPLY operator implements one logical query processing phase—it applies the right 

table expression to each row from the left table and produces a result table with the unified result sets.

So far it might sound like the CROSS APPLY operator is very similar to a cross join, and in a sense 

that’s true. For example, the following two queries return the same result sets.

SELECT S.shipperid, E.empid 

FROM Sales.Shippers AS S 

  CROSS JOIN HR.Employees AS E; 

 

SELECT S.shipperid, E.empid 



FROM Sales.Shippers AS S 

  CROSS APPLY HR.Employees AS E;

However, with the CROSS APPLY operator, the right table expression can represent a different set 

of rows per each row from the left table, unlike in a join. You can achieve this when you use a derived 

table in the right side, and in the derived table query refer to attributes from the left side. Alterna-

tively, when you use an inline TVF, you can pass attributes from the left side as input arguments.

For example, the following code uses the CROSS APPLY operator to return the three most recent 

orders for each customer.

SELECT C.custid, A.orderid, A.orderdate 

FROM Sales.Customers AS C 

  CROSS APPLY 

    (SELECT TOP (3) orderid, empid, orderdate, requireddate  

     FROM Sales.Orders AS O 

     WHERE O.custid = 



C.custid

 

     ORDER BY orderdate DESC, orderid DESC) AS A;



www.it-ebooks.info



Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   226   227   228   229   230   231   232   233   ...   443




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish