Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet208/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   204   205   206   207   208   209   210   211   ...   443
Bog'liq
BookSQL

  

157

C H A P T E R   5

Table expressions

A

 



table expression is a named query expression that represents a valid relational table. You 

can use table expressions in data manipulation statements much like you use other tables. 

Microsoft SQL Server supports four types of table expressions: derived tables, common table ex-

pressions (CTEs), views, and inline table-valued functions (inline TVFs), each of which I describe in 

detail in this chapter. The focus of this chapter is using SELECT queries against table expressions; 

Chapter 8, “Data Modification,” covers modifications against table expressions.

Table expressions are not physically materialized anywhere—they are virtual. When you query a 

table expression, the inner query gets unnested. In other words, the outer query and the inner query 

are merged into one query directly against the underlying objects. The benefits of using table expres-

sions are typically related to logical aspects of your code and not to performance. For example, table 

expressions help you simplify your solutions by using a modular approach. Table expressions also help 

you circumvent certain restrictions in the language, such as the inability to refer to column aliases as-

signed in the SELECT clause in query clauses that are logically processed before the SELECT clause.

This chapter also introduces the APPLY table operator as it is used in conjunction with a table ex-

pression. I explain how to use this operator to apply a table expression to each row of another table.

Derived Tables

Derived tables (also known as table subqueries) are defined in the FROM clause of an outer query. Their 

scope of existence is the outer query. As soon as the outer query is finished, the derived table is gone.

You specify the query that defines the derived table within parentheses, followed by the AS clause 

and the derived table name. For example, the following code defines a derived table called USACusts 

based on a query that returns all customers from the United States, and the outer query selects all 

rows from the derived table.

USE TSQL2012; 

 

SELECT * 



FROM (SELECT custid, companyname 

      FROM Sales.Customers 

      WHERE country = N'USA') AS USACusts;

www.it-ebooks.info




158  

Microsoft SQL Server 2012 T-SQL Fundamentals

In this particular case, which is a simple example of the basic syntax, a derived table is not needed 

because the outer query doesn’t apply any manipulation.

The code in this basic example returns the following output.

custid      companyname 

----------- --------------- 

32          Customer YSIQX 

36          Customer LVJSO 

43          Customer UISOJ 

45          Customer QXPPT 

48          Customer DVFMB 

55          Customer KZQZT 

65          Customer NYUHS 

71          Customer LCOUJ 

75          Customer XOJYP 

77          Customer LCYBZ 

78          Customer NLTYP 

82          Customer EYHKM 

89          Customer YBQTI

A query must meet three requirements to be valid to define a table expression of any kind:


Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   204   205   206   207   208   209   210   211   ...   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