Sql server® 2012 t-sql fundamentals



Download 10,93 Mb.
Pdf ko'rish
bet299/443
Sana26.01.2022
Hajmi10,93 Mb.
#411755
1   ...   295   296   297   298   299   300   301   302   ...   443
Bog'liq
BookSQL

 

CHAPTER 8

 

Data Modification



 

 251

The 

SELECT INTO

 Statement

The SELECT INTO statement is a nonstandard T-SQL statement that creates a target table and popu-

lates it with the result set of a query. By “nonstandard,” I mean that it is not part of the ISO and ANSI 

SQL standards. You cannot use this statement to insert data into an existing table. In terms of syntax, 

simply add INTO <target_table_name> right before the FROM clause of the SELECT query that you 

want to use to produce the result set. For example, the following code creates a table called dbo.Orders 

and populates it with all rows from the Sales.Orders table.

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

 

SELECT orderid, orderdate, empid, custid 



INTO dbo.Orders 

FROM Sales.Orders;

The target table’s structure and data are based on the source table. The SELECT INTO statement 

copies from the source the base structure (column names, types, nullability, and identity property) and 

the data. There are four things that the statement does not copy from the source: constraints, indexes, 

triggers, and permissions. If you need those in the target, you will need to create them yourself. 



note

  At the date of this writing, Windows Azure SQL Database doesn’t support heaps 

(tables without clustered indexes). SELECT INTO creates a heap because it doesn’t copy 

indexes— including clustered ones. For this reason, SQL Database doesn’t support SELECT 



INTO. You will need to issue a CREATE TABLE statement followed by an INSERT SELECT 

statement to achieve the same result.

One of the benefits of the SELECT INTO statement is that as long as a database property called Re-

covery Model is not set to FULL, the SELECT INTO operation is performed in a minimally logged mode. 

This translates to a very fast operation compared to a fully logged one. Note also that the INSERT 



SELECT statement can benefit from minimal logging, but the list of requirements it needs to meet is 

longer. For details, see “Prerequisites for Minimal Logging in Bulk Import” in SQL Server Books Online 

at the following URL: http://msdn.microsoft.com/en-us/library/ms190422.aspx.

If you need to use a SELECT INTO statement with set operations, you specify the INTO clause right 

in front of the FROM clause of the first query. For example, the following SELECT INTO statement 

creates a table called Locations and populates it with the result of an EXCEPT set operation, returning 

locations where there are customers but not employees.

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

 

SELECT country, region, city 



INTO dbo.Locations 

FROM Sales.Customers 

 

EXCEPT 


 

SELECT country, region, city 

FROM HR.Employees;

www.it-ebooks.info





Download 10,93 Mb.

Do'stlaringiz bilan baham:
1   ...   295   296   297   298   299   300   301   302   ...   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