在SQL Server中将所选数据从一个表复制到另一个表

时间:2016-10-12 16:09:39

标签: sql sql-server select sql-server-2012

我已经编写了查询来获取数据。

select Terr, Product 
from sdata, md 
where sdata.PSetID = md.PsetID;

我想将此获取的数据复制到新表中。

我尝试了以下查询,但它没有工作:

select * 
into ttl 
from 
    (select Terr, Product 
     from sdata, md 
     where sdata.PSetID = md.PsetID);

我做错了什么?

2 个答案:

答案 0 :(得分:3)

您需要将sub-select名称添加到SELECT * INTO ttl FROM (SELECT Terr, Product FROM sdata INNER JOIN md ON sdata.PSetID = md.PsetID) A; --Here

SELECT Terr,
       Product
INTO   ttl
FROM   sdata
       INNER JOIN md
               ON sdata.PSetID = md.PsetID 

或者只是

SELECT A.* FROM (
Select * FROM TABLE1
) A
WHERE (A.ColumnOne >= 18 OR ColumnOne IS NULL)
UNION
SELECT B.* FROM (
Select * FROM TABLE2
) B
WHERE (B.ColumnOne >= 18 OR ColumnOne IS NULL)
UNION
SELECT C.* FROM (
Select * FROM TABLE2
) C
WHERE (C.ColumnOne >= 18 OR ColumnOne IS NULL)

答案 1 :(得分:0)

另一个选项是使用以下语法:

SELECT column_name(s) INTO新表格 FROM table1;

在你的情况下:

  <div class="form-group col-md-12 input_fields_wrap">
          <label for="merk">Merk</label>
          <div class="input-group">
            <input type="text" class="form-control" name="merk[]">
            <span class="input-group-btn">
              <button class="btn btn-secondary add_field_button" type="button">add row</button>
            </span>
          </div>
          <div id="1"></div>
        </div>