使用3个不同表中的字段创建表错误

时间:2015-11-20 07:23:40

标签: sql tsql join create-table

我正在尝试创建一个由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行。我不明白为什么桌子会变大。

先谢谢

1 个答案:

答案 0 :(得分:1)

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

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

添加更多条件时,应该会解决此问题