Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet296/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   292   293   294   295   296   297   298   299   ...   443
Bog'liq
BookSQL

The 

INSERT VALUES

 Statement

You use the INSERT VALUES statement to insert rows into a table based on specified values. To prac-

tice using this statement and others, you will work with a table called Orders in the dbo schema in the 

TSQL2012 database. Run the following code to create the Orders table.

USE TSQL2012; 

 

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



 

CREATE TABLE dbo.Orders 

  orderid   INT         NOT NULL 



    CONSTRAINT PK_Orders PRIMARY KEY

  orderdate DATE        NOT NULL 

    CONSTRAINT DFT_orderdate DEFAULT(SYSDATETIME()), 

  empid     INT         NOT NULL, 

  custid    VARCHAR(10) NOT NULL 

)

www.it-ebooks.info




248  

Microsoft SQL Server 2012 T-SQL Fundamentals

The following example demonstrates how to use the INSERT VALUES statement to insert a single 

row into the Orders table.

INSERT INTO dbo.Orders(orderid, orderdate, empid, custid) 

  VALUES(10001, '20090212', 3, 'A');

Specifying the target column names right after the table name is optional, but by doing so, you 

control the value-column associations instead of relying on the order in which the columns appeared 

when the table was defined (or the table structure was last altered). 

If you specify a value for a column, Microsoft SQL Server will use that value. If you don’t, SQL Server 

will check whether a default value is defined for the column, and if so, the default will be used. If a de-

fault value isn’t defined and the column allows NULL marks, a NULL will be used. If you do not specify 

a value for a column that does not allow NULL marks and does not somehow get its value automati-

cally, your INSERT statement will fail. As an example of relying on a default value or expression, the 

following statement inserts a row into the Orders table without specifying a value for the orderdate 

column, but because this column has a default expression defined for it (SYSDATETIME), that default 

will be used.

INSERT INTO dbo.Orders(orderid, empid, custid) 

  VALUES(10002, 5, 'B');

SQL Server 2008 and SQL Server 2012 support an enhanced VALUES clause that allows you to 

specify multiple rows separated by commas. For example, the following statement inserts four rows 

into the Orders table.

INSERT INTO dbo.Orders 

  (orderid, orderdate, empid, custid) 

VALUES 

  (10003, '20090213', 4, 'B'), 

  (10004, '20090214', 1, 'A'), 

  (10005, '20090213', 1, 'C'), 

  (10006, '20090215', 3, 'C');

This statement is processed as an atomic operation, meaning that if any row fails to enter the table, 

none of the rows in the statement enters the table.

There’s more to this enhanced VALUES clause. You can use it in a standard way as a table value 

constructor to construct a derived table. Here’s an example.

SELECT * 

FROM ( VALUES 

         (10003, '20090213', 4, 'B'), 

         (10004, '20090214', 1, 'A'), 

         (10005, '20090213', 1, 'C'), 

         (10006, '20090215', 3, 'C') ) 

     AS O(orderid, orderdate, empid, custid);

www.it-ebooks.info




Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   292   293   294   295   296   297   298   299   ...   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