cnblogs/dcrenl/SELECT INTO 和 INSERT INTO SELECT 两种表复制语句.html
2024-09-24 12:43:01 +08:00

105 lines
33 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div id="sina_keyword_ad_area2" class="articalContent ">
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
Insert是T-sql中常用语句Insert INTO table(field1,field2,...)values(value1,value2,...)这种形式的在应用程序开发中必不可少。但我们在开发、测试过程中经常会遇到需要表复制的情况如将一个table1的数据的部分字段复制到table2中或者将整个table1复制到table2中这时候我们就要使用<span style="font-family:Verdana">SELECTINTO 和 INSERT INTO SELECT 表复制语句了。</span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>1.<span style="font-family:Verdana">INSERT INTO SELECT语句</span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>语句形式为:<span style="font-family:Verdana"><span style="color:rgb(255,0,0)"><span style="font-family:Verdana">Insertinto Table2(field1,field2,...) select value1,value2,... fromTable1</span></span></span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span style="font-family:Verdana">要求目标表Table2必须存在由于目标表Table2已经存在所以我们除了插入源表Table1的字段外还可以插入常量。示例如下</span></p>
<div style="font-family:'Courier new'; word-wrap:break-word; background-color:rgb(245,245,245); font-size:12px; border:1px solid rgb(204,204,204); padding:5px; overflow:auto; margin:5px 0px; line-height:22px">
<img height="16" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="11" align="top" name="Code_Open_Image_080803" alt="SELECT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;和&nbsp;&lt;wbr&gt;INSERT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;SELECT&nbsp;&lt;wbr&gt;两种表复制语句" title="SELECT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;和&nbsp;&lt;wbr&gt;INSERT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;SELECT&nbsp;&lt;wbr&gt;两种表复制语句" style="display:inline"><span style="font-family:'Courier new'; display:inline"><br>
<span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">1.创建测试表</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">create</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">TABLE</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>(<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>a&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>b&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>c&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">CONSTRAINT</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">[</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">PK_Table1</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">]</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">PRIMARY</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">KEY</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">CLUSTERED</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>(<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>a&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">ASC</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>)&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">ON</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">[</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">PRIMARY</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">]</span><span style="font-family:'Courier new'"><br>
<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">create</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">TABLE</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>(<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>a&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>c&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>d&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">int</span><span style="font-family:'Courier new'">,<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">CONSTRAINT</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">[</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">PK_Table2</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">]</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">PRIMARY</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">KEY</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">CLUSTERED</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>(<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>a&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">ASC</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>)&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">ON</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">[</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">PRIMARY</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">]</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">2.创建测试数据</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">90</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">100</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">80</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(0,0,255)">null</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">select</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(128,128,128)">*</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">from</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2<br>
<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">3.INSERT&nbsp;<wbr>INTO&nbsp;<wbr>SELECT语句复制表数据</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2(a,&nbsp;<wbr>c,&nbsp;<wbr>d)&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">select</span><span style="font-family:'Courier new'">&nbsp;<wbr>a,c,</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>5</strong></span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">from</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">4.显示更新后的结果</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">select</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(128,128,128)">*</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">from</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">5.删除测试表</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">drop</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">TABLE</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">drop</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">TABLE</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2</span></span></div>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>2.<span style="font-family:Verdana">SELECT INTO FROM语句</span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>语句形式为:<span style="font-family:Verdana"><span style="color:rgb(255,0,0)"><span style="font-family:Verdana">SELECTvale1, value2 into Table2 from Table1</span></span></span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span style="font-family:Verdana">要求目标表Table2不存在<span style="font-family:Verdana">因为在插入时会自动创建表Table2并将Table1中指定字段数据复制到Table2中</span>。示例如下:</span></p>
<div style="font-family:'Courier new'; word-wrap:break-word; background-color:rgb(245,245,245); font-size:12px; border:1px solid rgb(204,204,204); padding:5px; overflow:auto; margin:5px 0px; line-height:22px">
<img height="16" src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif" width="11" align="top" name="Code_Open_Image_082138" alt="SELECT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;和&nbsp;&lt;wbr&gt;INSERT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;SELECT&nbsp;&lt;wbr&gt;两种表复制语句" title="SELECT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;和&nbsp;&lt;wbr&gt;INSERT&nbsp;&lt;wbr&gt;INTO&nbsp;&lt;wbr&gt;SELECT&nbsp;&lt;wbr&gt;两种表复制语句" style="display:inline"><span style="font-family:'Courier new'; display:inline"><br>
<span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">1.创建测试表</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">create</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">TABLE</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>(<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>a&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>b&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>c&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">varchar</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(128,0,0)"><strong>10</strong></span><span style="font-family:'Courier new'">),<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">CONSTRAINT</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">[</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">PK_Table1</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">]</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">PRIMARY</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">KEY</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">CLUSTERED</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>(<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>a&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">ASC</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>)&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">ON</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">[</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">PRIMARY</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">]</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">2.创建测试数据</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">90</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">100</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">80</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">Insert</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">into</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">values</span><span style="font-family:'Courier new'">(</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)"></span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">asds</span><span style="font-family:'Courier new'; color:rgb(255,0,0)">'</span><span style="font-family:'Courier new'">,</span><span style="font-family:'Courier new'; color:rgb(0,0,255)">null</span><span style="font-family:'Courier new'">)<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">3.SELECT&nbsp;<wbr>INTO&nbsp;<wbr>FROM语句创建表Table2并复制数据</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">select</span><span style="font-family:'Courier new'">&nbsp;<wbr>a,c&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">INTO</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">from</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">4.显示更新后的结果</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">select</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(128,128,128)">*</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">from</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">GO</span><span style="font-family:'Courier new'"><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,128,128)">--</span><span style="font-family:'Courier new'; color:rgb(0,128,128)">5.删除测试表</span><span style="font-family:'Courier new'; color:rgb(0,128,128)"><br>
</span><span style="font-family:'Courier new'">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">drop</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">TABLE</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table1<br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">drop</span><span style="font-family:'Courier new'">&nbsp;<wbr></span><span style="font-family:'Courier new'; color:rgb(0,0,255)">TABLE</span><span style="font-family:'Courier new'">&nbsp;<wbr>Table2</span></span></div>
<p style="margin:5px auto; line-height:22px; background-color:rgb(255,255,255)"><span style="font-family:verdana">3.</span><span style="font-family:Verdana">使用IDENTITY</span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>语句形式1<span style="font-family:Verdana"><span style="color:rgb(255,0,0)"><span style="font-family:Verdana">SELECTidentity(int,1,1) as c1, &nbsp;<wbr>c2, c3 into Table2 fromTable1</span></span></span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
<span style="font-family:verdana; line-height:22px">&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr> 语句形式2</span><span style="font-family:Verdana"><span style="color:rgb(255,0,0)"><span style="font-family:Verdana">Insert into Table2(c1,c2) select identity(int,1,1) asc1,c2 from Table1</span></span></span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
<span style="font-family:Verdana"><span style="color:rgb(255,0,0)"><span style="font-family:Verdana"><br>
</span></span></span></p>
<p style="margin:5px auto; font-family:verdana; line-height:22px; background-color:rgb(255,255,255)">
<span style="font-family:verdana; line-height:22px">&nbsp;<wbr>本文转自:</span><a target="_blank" href="http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html" target="_blank">http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html</a></p>
</div>