码迷,mamicode.com
首页 > 其他好文 > 详细

select into clause

时间:2015-09-11 19:20:50      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

使用select into能够复制源表的结构创建一个新表,同时向表中插入数据。在SELECT INTO 复制表结构的时候,只是复制了源表的列名,列的数据类型,null,Identity,但源表的主键、外键、约束、触发器、索引都不会被复制过来。

复制一个表结构,也可以使用Create table+insert into这种方法, 如果在simple和bulk logged 恢复模式下,select into的性能更高,因为select into是一个bulk logged操作,sql使用最小日志记录,性能比insert into高。

 

Select Ino Syntax

select column_list
into new_table
from source_table

new_table is created based on the columns in the select list and the rows chosen from the data source.

The format of new_table is determined by evaluating the expressions in the select list. The columns in new_table are created in the order specified by the select list. Each column in new_table has the same name, data type, nullability, and value as the corresponding expression in the select list. The IDENTITY property of a column is transferred except under the conditions defined in the below section.

When an existing identity column is selected into a new table, the new column inherits the IDENTITY property, unless one of the following conditions is true: 

  • The SELECT statement contains a join.

  • Multiple SELECT statements are joined by using UNION.

  • The identity column is listed more than one time in the select list.

  • The identity column is part of an expression.

  • The identity column is from a remote data source.

If any one of these conditions is true, the column is created NOT NULL instead of inheriting the IDENTITY property. If an identity column is required in the new table but such a column is not available, or you want a seed or increment value that is different than the source identity column, define the column in the select list using the IDENTITY function.

 

Limitations and Restrictions

You cannot specify a table variable or table-valued parameter as the new table.

You cannot use SELECT…INTO to create a partitioned table, even when the source table is partitioned. SELECT...INTO does not use the partition scheme of the source table; instead, the new table is created in the default filegroup. To insert rows into a partitioned table, you must first create the partitioned table and then use the INSERT INTO...SELECT FROM statement.

Indexes, constraints, and triggers defined in the source table are not transferred to the new table, nor can they be specified in the SELECT...INTO statement. If these objects are required, you can create them after executing the SELECT...INTO statement.

Specifying an ORDER BY clause does not guarantee the rows are inserted in the specified order.

When a sparse column is included in the select list, the sparse column property does not transfer to the column in the new table. If this property is required in the new table, alter the column definition after executing the SELECT...INTO statement to include this property.

When a computed column is included in the select list, the corresponding column in the new table is not a computed column. The values in the new column are the values that were computed at the time SELECT...INTO was executed.

 

Logging Behavior

The amount of logging for SELECT...INTO depends on the recovery model in effect for the database. Under the simple recovery model or bulk-logged recovery model, bulk operations are minimally logged. With minimal logging, using the SELECT… INTO statement can be more efficient than creating a table and then populating the table with an INSERT statement.

 

示例,select into 和identity 列

1,创建实验表

CREATE TABLE [dbo].[ta]
(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](10) NULL
)


CREATE TABLE [dbo].[tb]
(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](10) NULL
)


2,使用不带join的select into clause创建新表,发现td的id列是存在Identity属性的

select top 0 id
into dbo.td
from dbo.ta

技术分享

3,使用带 join的select into clause创建新表,发现tc的AID和BID是没有Identity属性的。

select    top 0 
    a.id as AID ,b.id as BID
into dbo.tc
from dbo.ta a
inner join dbo.tb b
    on a.id=b.id

技术分享

 

4,使用Identity函数创建ID列

select    top 0 
    a.id as AID ,b.id as BID, 
    identity(int,1,1) as CID
into dbo.te
from dbo.ta a
inner join dbo.tb b
    on a.id=b.id

技术分享

 

5,使用sys.identity_columns 视图来查看表的ID列信息

select o.name as ObjectName,o.type_desc as ObjectType,
    c.name as ColumnName,c.is_identity
from sys.objects o
left join sys.identity_columns c 
    on o.object_id=c.object_id
where o.name in(ta,tb,tc,td,te)

技术分享

 

select into clause

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4801711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!