简体   繁体   English

将数据从SQL Server表导入另一个表

[英]Import data from SQL Server table into another table

I can't get this SQL statement right. 我无法正确执行此SQL语句。 I want to select band emails into another table but I don't understand what I'm doing wrong: 我想将乐队电子邮件选择到另一个表中,但是我不明白我在做什么错:

INSERT INTO submitted (mail)
    SELECT
        submitted.bandid,
        band.mail,
        band.id
    FROM 
        band
    WHERE 
        submitted.bandid = band.id

Can anybody help me? 有谁能够帮助我?

Try this, you had specified 1 column to INSERT INTO but your SELECT query contained 3 columns, also the tables in your weren't joined: 尝试此操作,您已向INSERT INTO指定了1列,但是SELECT查询包含3列,而且您的表也未联接:

INSERT INTO submitted (mail) 
SELECT
band.mail
FROM band 
INNER JOIN submitted ON submitted.bandid = band.id

You haven't mentioned the error message you're getting, but I suspect the problem is that you only have one column specified in the INSERT part of the statement, but the SELECT part returns many columns. 您没有提到收到的错误消息,但是我怀疑问题是您在语句的INSERT部分中仅指定了一个列,但是SELECT部分返回了许多列。

This w3schools page has a good example of how this query should be structured: 这个w3schools页面上有一个很好的示例,说明了如何构造此查询:

Copy only some columns from one table into another table: 仅将某些列从一个表复制到另一表:

 INSERT INTO table2 (column1, column2, column3, ...) SELECT column1, column2, column3, ... FROM table1 WHERE condition; 

I suspect you want update , not insert : 我怀疑您要update ,而不要insert

UPDATE s
    SET mail = b.mail
    FROM submitted s JOIN
         band b
         ON s.bandid = b.id;

insert inserts new rows into the table. insert将新插入表中。 update updates columns in existing rows. update更新现有行中列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM