Sql server® 2012 t-sql fundamentals



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

 

CHAPTER 5

 

Table Expressions



 

 181

IF OBJECT_ID('dbo.TopOrders') IS NOT NULL 

  DROP FUNCTION dbo.TopOrders; 

GO 


CREATE FUNCTION dbo.TopOrders 

  (@custid AS INT, @n AS INT) 

  RETURNS TABLE 

AS 


RETURN 

  SELECT TOP (@n) orderid, empid, orderdate, requireddate  

  FROM Sales.Orders 

  WHERE custid = @custid 

  ORDER BY orderdate DESC, orderid DESC; 

GO

By using OFFSET-FETCH instead of TOP, you can replace the inner query in the function with this one.



  SELECT orderid, empid, orderdate, requireddate  

  FROM Sales.Orders 

  WHERE custid = @custid 

  ORDER BY orderdate DESC, orderid DESC 

  OFFSET 0 ROWS FETCH FIRST @n ROWS ONLY;

You can now substitute the use of the derived table from the previous examples with the new 

function.

SELECT 


  C.custid, C.companyname, 

  A.orderid, A.empid, A.orderdate, A.requireddate  

FROM Sales.Customers AS C 

  CROSS APPLY dbo.TopOrders(C.custid, 3) AS A;

The code is much more readable and easier to maintain. In terms of physical processing, noth-

ing really changed because, as I stated earlier, the definition of table expressions is expanded, and 

SQL Server will in any case end up querying the underlying objects directly.

Conclusion

Table expressions can help you simplify your code, improve its maintainability, and encapsulate que-

rying logic. When you need to use table expressions and are not planning to reuse their definitions, 

use derived tables or CTEs. CTEs have a couple of advantages over derived tables; you do not nest 

CTEs as you do derived tables, making CTEs more modular and easier to maintain. Also, you can refer 

to multiple instances of the same CTE, which you cannot do with derived tables.

When you need to define reusable table expressions, use views or inline TVFs. When you do not 

need to support input parameters, use views; otherwise, use inline TVFs.

Use the APPLY operator when you want to apply a table expression to each row from a source 

table and unify all result sets into one result table.

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   228   229   230   231   232   233   234   235   ...   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