Sql server® 2012 t-sql fundamentals


   Microsoft SQL Server 2012 T-SQL Fundamentals 5-1



Download 10,93 Mb.
Pdf ko'rish
bet241/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   237   238   239   240   241   242   243   244   ...   443
Bog'liq
BookSQL

190  

Microsoft SQL Server 2012 T-SQL Fundamentals



5-1

This exercise requires you to define a function called TopProducts that accepts a supplier ID (@supid) 

and a number (@n), and is supposed to return the @n most expensive products supplied by the input 

supplier ID. Here’s how the function definition should look.

USE TSQL2012; 

IF OBJECT_ID('Production.TopProducts') IS NOT NULL 

  DROP FUNCTION Production.TopProducts; 

GO 


CREATE FUNCTION Production.TopProducts 

  (@supid AS INT, @n AS INT) 

  RETURNS TABLE 

AS 


RETURN 

  SELECT TOP (@n) productid, productname, unitprice 

  FROM Production.Products 

  WHERE supplierid = @supid 

  ORDER BY unitprice DESC; 

GO

Starting with SQL Server 2012, you can use the OFFSET-FETCH filter instead of TOP. You would 



replace the inner query in the function with the following one.

  SELECT productid, productname, unitprice 

  FROM Production.Products 

  WHERE supplierid = @supid 

  ORDER BY unitprice DESC 

  OFFSET 0 ROWS FETCH FIRST @n ROWS ONLY;



5-2

In this exercise, you write a query against the Production.Suppliers table and use the CROSS APPLY op-

erator to apply the function you defined in the previous step to each supplier. Your query is supposed 

to return the two most expensive products for each supplier. Here’s the solution query.

SELECT S.supplierid, S.companyname, P.productid, P.productname, P.unitprice 

FROM Production.Suppliers AS S 

  CROSS APPLY Production.TopProducts(S.supplierid, 2) AS P;

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   237   238   239   240   241   242   243   244   ...   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