简体   繁体   English

MSSQL选择更多表以进行插入

[英]MSSQL Select more Tables to INSERT

I'm struggling with this piece of code: 我在这段代码中苦苦挣扎:

INSERT INTO Bier(Bier, BrowuerID, TypeID, GistingID, KleurID, Alcohol)
SELECT bb.Naam, b.BrouwerID, t.TypeID, g.GistingID, k.KleurID, bb.Alcoholperc
FROM BELGISCHBIER AS bb JOIN Brouwer AS b JOIN Type AS t JOIN Gisting AS g JOIN Kleur AS k

Can someone tell why this won't work because I don't really know that much about SQL 有人可以告诉我为什么这行不通,因为我对SQL不太了解

Thanks. 谢谢。

You have used JOINS without the mandatory ON clause which determines on what columns the tables should be joined: 您使用了JOINS但没有强制性的ON子句,该子句确定应在哪些列上连接表:

INSERT INTO bier 
            ( 
                        bier, 
                        browuerid, 
                        typeid, 
                        gistingid, 
                        kleurid, 
                        alcohol 
            ) 
SELECT bb.naam, 
       b.brouwerid, 
       t.typeid, 
       g.gistingid, 
       k.kleurid, 
       bb.alcoholperc 
FROM   belgischbier AS bb 
JOIN   brouwer      AS b 
    ON bb.browuerid= b.browuerid
JOIN   type         AS t 
    ON b.BrouwerTypeID = t.BrouwerTypeID
JOIN   gisting      AS g 
    ON bb.GistingID = g.GistingID
JOIN   kleur        AS k
    ON g.KleurID = k.KleurID  

with JOIN , SQL understand INNER JOIN . 使用JOIN ,SQL可以了解INNER JOIN So is mandatory use of ON clause. 因此强制使用ON子句。

Only if you use CROSS JOIN you can don't write ON clause. 仅当您使用CROSS JOIN您才能编写ON子句。

So, add ON clause in your JOIN command. 因此,在JOIN命令中添加ON子句。

Your code has some syntatical error , You missed the On clause In Join operation 您的代码有一些语法错误,您错过了Join操作中的On子句

try somethink like this, 尝试这样的想法,

INSERT INTO Bier(Bier, BrowuerID, TypeID, GistingID, KleurID, Alcohol)
SELECT bb.Naam, b.BrouwerID, t.TypeID, g.GistingID, k.KleurID, bb.Alcoholperc
FROM BELGISCHBIER AS bb <on Clause>JOIN Brouwer  AS b  <on Clause> JOIN Type on  AS t <on Clause> JOIN Gisting AS g  <on Clause>  JOIN  Kleur AS k  <on Clause>

Or simply you can use Cross Join ,it will not need On clause. 或者简单地,您可以使用Cross Join,它将不需要On子句。

Syntax : 句法 :

SELECT * 
FROM table1 
CROSS JOIN table2;

Example : 范例:

INSERT INTO Bier
            (Bier,BrowuerID,TypeID,GistingID,
             KleurID,Alcohol)
SELECT bb.Naam,b.BrouwerID,t.TypeID,g.GistingID,
       k.KleurID,bb.Alcoholperc
FROM   BELGISCHBIER AS bb
       CROSS JOIN Brouwer AS b
       CROSS JOIN Type AS t
       CROSS JOIN Gisting AS g
       CROSS JOIN Kleur AS k 

If you don't have common columns between the tables use Cross Join else as mentioned by others you need to use ON condition to Join two tables 如果表之间没有公共列,请使用Cross Join ,如其他人所述,您需要使用ON条件连接两个表

INSERT INTO Bier
            (Bier,BrowuerID,TypeID,GistingID,
             KleurID,Alcohol)
SELECT bb.Naam,b.BrouwerID,t.TypeID,g.GistingID,
       k.KleurID,bb.Alcoholperc
FROM   BELGISCHBIER AS bb
       CROSS JOIN Brouwer AS b
       CROSS JOIN Type AS t
       CROSS JOIN Gisting AS g
       CROSS JOIN Kleur AS k 

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

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