Sql server® 2012 t-sql fundamentals


a Batch as a Unit of resolution



Download 10,93 Mb.
Pdf ko'rish
bet376/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   372   373   374   375   376   377   378   379   ...   443
Bog'liq
BookSQL

a Batch as a Unit of resolution

A batch is a unit of resolution. This means that checking the existence of objects and columns hap-

pens at the batch level. Keep this fact in mind when you are designing batch boundaries. When you 

apply schema changes to an object and try to manipulate the object data in the same batch, SQL 

Server might not be aware of the schema changes yet and fail the data manipulation statement with 

a resolution error. I’ll demonstrate the problem through an example and then recommend best prac-

tices.

Run the following code to create a table called T1 in the current database, with one column 



called col1.

IF OBJECT_ID('dbo.T1', 'U') IS NOT NULL DROP TABLE dbo.T1; 

CREATE TABLE dbo.T1(col1 INT);

Next, try to add a column called col2 to T1 and query the new column in the same batch.

ALTER TABLE dbo.T1 ADD col2 INT; 

SELECT col1, col2 FROM dbo.T1;

Even though the code might seem to be perfectly valid, the batch fails during the resolution phase 

with the following error.

Msg 207, Level 16, State 1, Line 2 

Invalid column name 'col2'.

At the time the SELECT statement was resolved, T1 had only one column, and the reference to the 

col2 column caused the error. One best practice you can follow to avoid such problems is to separate 

DDL and DML statements into different batches, as in the following example.

ALTER TABLE dbo.T1 ADD col2 INT; 

GO 


SELECT col1, col2 FROM dbo.T1;


Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   372   373   374   375   376   377   378   379   ...   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