【问题标题】:Creating table with fields from 3 different tables Error使用来自 3 个不同表的字段创建表错误
【发布时间】:2016-02-22 14:38:51
【问题描述】:

我正在尝试创建一个由 3 个其他表组合而成的表(但由于关系需要加入 4 个)。我是从这里关注帖子的:

Creating tables with fields from 2 different tables

当我在没有CREATE TABLE Customer_Information AS 的情况下运行查询时,我没有收到任何错误并向我显示表格。但是当我用CREATE 运行它时,我得到了这个错误:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.

这里是查询:

CREATE TABLE Customer_Information AS (
SELECT DimServere.Servernavn, DimServere.Serverstatus,
       DimKunder.Ministerium, DimKunder.MinisteriumFuldeNavn, DimKunder.RapporteringsKunde, 
       IderaPatchAnalyzer.IP_Adresse, IderaPatchAnalyzer.Release_, IderaPatchAnalyzer.Level_, IderaPatchAnalyzer.Edition_, 
       IderaPatchAnalyzer.Build, IderaPatchAnalyzer.Updates_Available, IderaPatchAnalyzer.Supported_, IderaPatchAnalyzer.Support_Status

FROM IderaPatchAnalyzer 
        JOIN DimServere
            ON IderaPatchAnalyzer.IP_Adresse = DimServere.TcpIpAddress
        JOIN FactSystemServereKunder
            ON DimServere.Servernavn = FactSystemServereKunder.Servernavn
        JOIN DimKunder
            On FactSystemServereKunder.KundeID = DimKunder.KundeID
        WHERE DimServere.Serverstatus != 'Disposed/Retired'
        );

同样在表IderaPatchAnalyzer 中,当我做一个简单的SELECT * FROM IderaPatchAnalyzer 时,我得到190 行。但是当我运行连接表时,如上所示,我得到 437 行。我的目标是将信息附加到这 190 行。我不明白为什么桌子变大了。

在此致谢

【问题讨论】:

    标签: sql tsql join create-table


    【解决方案1】:

    我不认为,你可以这样初始化表。 您可以使用:

    Select * Into #(tableName)From X where X = Y
    

    但是你需要删除那个表

    Drop Table #(tableName)
    

    同样重要的是,它会说它不知道那个表,但你可以毫无问题地使用它。

    您的代码将如下所示:

    SELECT DimServere.Servernavn, DimServere.Serverstatus,
           DimKunder.Ministerium, DimKunder.MinisteriumFuldeNavn, DimKunder.RapporteringsKunde, 
           IderaPatchAnalyzer.IP_Adresse, IderaPatchAnalyzer.Release_, IderaPatchAnalyzer.Level_, IderaPatchAnalyzer.Edition_, 
           IderaPatchAnalyzer.Build, IderaPatchAnalyzer.Updates_Available, IderaPatchAnalyzer.Supported_, IderaPatchAnalyzer.Support_Status
    INTO #Customer_Information 
    FROM IderaPatchAnalyzer 
            JOIN DimServere
                ON IderaPatchAnalyzer.IP_Adresse = DimServere.TcpIpAddress
            JOIN FactSystemServereKunder
                ON DimServere.Servernavn = FactSystemServereKunder.Servernavn
            JOIN DimKunder
                On FactSystemServereKunder.KundeID = DimKunder.KundeID
            WHERE DimServere.Serverstatus != 'Disposed/Retired'
    
            --Your Code
    
            DROP TABLE #Customer_Information
    

    为您的下一个问题。您对 1 项有多个引用。当您向JOIN / WHERE添加更多条件时应该可以解决它

    【讨论】:

    • 我测试了您所说的内容,而不是#Customer_Information,只输入了Customer_Information,并实际创建了一个包含这些信息的表。然后我可以更改表并添加 IP_Adresse 主键。这是一种公认​​的方法吗?或者这是一个很大的不去?
    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多