Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet187/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   183   184   185   186   187   188   189   190   ...   443
Bog'liq
BookSQL

 

CHAPTER 4

 

Subqueries



 

 131

from both sides, the subquery is considered scalar. Because the subquery can potentially return 

more than one value, the choices of using an equality operator and a scalar subquery here are 

wrong. If the subquery returns more than one value, the query fails. 

This query happens to run without failure because currently the Employees table contains only one 

employee whose last name starts with B (Sven Buck with employee ID 5). This query returns the fol-

lowing output, shown here in abbreviated form.

orderid 


----------- 

10248 


10254 

10269 


10297 

10320 


... 

10874 


10899 

10922 


10954 

11043 


 

(42 row(s) affected)

Of course, if the subquery returns more than one value, the query fails. For example, try running 

the query with employees whose last names start with D.

SELECT orderid 

FROM Sales.Orders 

WHERE empid =  

  (SELECT E.empid 

   FROM HR.Employees AS E 

   WHERE E.lastname LIKE N'D%');

Apparently, two employees have a last name starting with D (Sara Davis and Zoya Dolgopyatova). 

Therefore, the query fails at run time with the following error.

Msg 512, Level 16, State 1, Line 1 

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, 



<= , >, >= or when the subquery is used as an expression.

If a scalar subquery returns no value, it returns a NULL. Recall that a comparison with a NULL 

yields UNKNOWN and that query filters do not return a row for which the filter expression evaluates 

to UNKNOWN. For example, the Employees table currently has no employees whose last names start 

with A; therefore, the following query returns an empty set.

SELECT orderid 

FROM Sales.Orders 

WHERE empid =  

  (SELECT E.empid 

   FROM HR.Employees AS E 

   WHERE E.lastname LIKE N'A%');

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   183   184   185   186   187   188   189   190   ...   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