Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet100/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   96   97   98   99   100   101   102   103   ...   443
Bog'liq
BookSQL

 

CHAPTER 2

 

Single-Table Queries



 

 57

Out of the 91 rows in the Customers table, the query returns the three rows where the region 

attribute is equal to WA. The query returns neither rows in which the value in the region attribute is 

present and different than WA (the predicate evaluates to FALSE) nor those where the region attribute 

is NULL (the predicate evaluates to UNKNOWN).

The following query attempts to return all customers for whom the region is different than WA. 

SELECT custid, country, region, city 

FROM Sales.Customers 

WHERE region <> N'WA';

This query generates the following output:

custid      country         region          city 

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

10          Canada          BC              Tsawassen 

15          Brazil          SP              Sao Paulo 

21          Brazil          SP              Sao Paulo 

31          Brazil          SP              Campinas 

32          USA             OR              Eugene 

33          Venezuela       DF              Caracas 

34          Brazil          RJ              Rio de Janeiro 

35          Venezuela       Táchira         San Cristóbal 

36          USA             OR              Elgin 

37          Ireland         Co. Cork        Cork 

38          UK              Isle of Wight   Cowes 

42          Canada          BC              Vancouver 

45          USA             CA              San Francisco 

46          Venezuela       Lara            Barquisimeto 

47          Venezuela       Nueva Esparta   I. de Margarita 

48          USA             OR              Portland 

51          Canada          Québec          Montréal 

55          USA             AK              Anchorage 

61          Brazil          RJ              Rio de Janeiro 

62          Brazil          SP              Sao Paulo 

65          USA             NM              Albuquerque 

67          Brazil          RJ              Rio de Janeiro 

71          USA             ID              Boise 

75          USA             WY              Lander 

77          USA             OR              Portland 

78          USA             MT              Butte 

81          Brazil          SP              Sao Paulo 

88          Brazil          SP              Resende 

 

(28 row(s) affected)



If you expected to get 88 rows back (91 rows in the table minus 3 returned by the previous query), 

you might find the fact that this query returned only 28 rows surprising. But remember, a query filter 

“accepts TRUE,” meaning that it rejects both rows for which the logical expression evaluates to FALSE 

and those for which it evaluates to UNKNOWN. So this query returned rows in which a value was pres-

ent in the region attribute and that value was different than WA. It returned neither rows in which the 

region attribute was equal to WA nor rows in which region was NULL. You will get the same output if 

you use the predicate NOT (region = N’WA’) because in the rows where region is NULL and the expres-

sion region = N’WA’ evaluates to UNKNOWNNOT (region = N’WA’) evaluates to UNKNOWN also.

www.it-ebooks.info




58  

Microsoft SQL Server 2012 T-SQL Fundamentals

If you want to return all rows for which region is NULL, do not use the predicate region = NULL

because the expression evaluates to UNKNOWN in all rows—both those in which the value is present 

and those in which the value is missing (is NULL). The following query returns an empty set.

SELECT custid, country, region, city 

FROM Sales.Customers 

WHERE region = NULL; 

 

custid      country         region          city 



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

 

(0 row(s) affected)



Instead, you should use the IS NULL predicate.

SELECT custid, country, region, city 

FROM Sales.Customers 

WHERE region IS NULL;

This query generates the following output, shown in abbreviated form.

custid      country         region          city 

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

1           Germany         NULL            Berlin 

2           Mexico          NULL            México D.F. 

3           Mexico          NULL            México D.F. 

4           UK              NULL            London 

5           Sweden          NULL            Luleå 

6           Germany         NULL            Mannheim 

7           France          NULL            Strasbourg 

8           Spain           NULL            Madrid 

9           France          NULL            Marseille 

11          UK              NULL            London 

... 


 

(60 row(s) affected)

If you want to return all rows for which the region attribute is not WA, including those in which the 

value is present and different than WA, along with those in which the value is missing, you need to 

include an explicit test for NULL marks, like this.

SELECT custid, country, region, city 

FROM Sales.Customers 

WHERE region <> N'WA' 

   OR region IS NULL;

This query generates the following output, shown in abbreviated form.

custid      country         region          city 

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

1           Germany         NULL            Berlin 

2           Mexico          NULL            México D.F. 

3           Mexico          NULL            México D.F. 

4           UK              NULL            London 

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   96   97   98   99   100   101   102   103   ...   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