2017-03-24 37 views
1

例如,我想在sql server中創建臨時表,並查詢下面的查詢結果,因爲我已經在下面嘗試查詢但我發現了'該列「僱員」被多次指定爲「ResultSet`請help.thanks如何在sql server中創建臨時表與多個連接表的結果集

SELECT * 
INTO #TempTable 
FROM 
(
SELECT 
    Employees.EmployeeID 
    ,Employees.LastName 
    ,Employees.FirstName 
    ,Employees.Title 
    ,Employees.TitleOfCourtesy 
    ,Employees.BirthDate 
    ,Employees.HireDate 
    ,Employees.Address 
    ,Employees.City 
    ,Employees.Region 
    ,Employees.PostalCode 
    ,Employees.Country 
    ,Employees.HomePhone 
    ,Employees.Extension 
    ,Employees.Photo 
    ,Employees.Notes 
    ,Employees.ReportsTo 
    ,Employees.PhotoPath 
    ,Orders.OrderID 
    ,Orders.CustomerID 
    ,Orders.EmployeeID 
    ,Orders.OrderDate 
    ,Orders.RequiredDate 
    ,Orders.ShippedDate 
    ,Orders.ShipVia 
    ,Orders.Freight 
    ,Orders.ShipName 
    ,Orders.ShipAddress 
    ,Orders.ShipCity 
    ,Orders.ShipRegion 
    ,Orders.ShipPostalCode 
    ,Orders.ShipCountry 
    ,Shippers.ShipperID 
    ,Shippers.CompanyName 
    ,Shippers.Phone 
    ,[Order Details].OrderID 
    ,[Order Details].ProductID 
    ,[Order Details].UnitPrice 
    ,[Order Details].Quantity 
    ,[Order Details].Discount 
    ,Products.ProductID 
    ,Products.ProductName 
    ,Products.SupplierID 
    ,Products.CategoryID 
    ,Products.QuantityPerUnit 
    ,Products.UnitPrice 
    ,Products.UnitsInStock 
    ,Products.UnitsOnOrder 
    ,Products.ReorderLevel 
    ,Products.Discontinued 
    ,Categories.CategoryID 
    ,Categories.CategoryName 
    ,Categories.Description 
    ,Categories.Picture 
    ,Suppliers.SupplierID 
    ,Suppliers.CompanyName 
    ,Suppliers.ContactName 
    ,Suppliers.ContactTitle 
    ,Suppliers.Address 
    ,Suppliers.City 
    ,Suppliers.Region 
    ,Suppliers.PostalCode 
    ,Suppliers.Country 
    ,Suppliers.Phone 
    ,Suppliers.Fax 
    ,Suppliers.HomePage 
FROM (((((((Northwind.Dbo.Products Products 
            INNER JOIN 
            Northwind.Dbo.Categories Categories 
            ON (Products.CategoryID = Categories.CategoryID)) 
           INNER JOIN 
           Northwind.Dbo.[Order Details] [Order Details] 
           ON ([Order Details].ProductID = Products.ProductID)) 
          INNER JOIN 
          Northwind.Dbo.Orders Orders 
          ON ([Order Details].OrderID = Orders.OrderID)) 
         INNER JOIN 
         Northwind.Dbo.Customers Customers 
         ON (Orders.CustomerID = Customers.CustomerID)) 
        INNER JOIN 
        Northwind.Dbo.Employees Employees 
        ON (Orders.EmployeeID = Employees.EmployeeID)) 
       INNER JOIN 
       Northwind.Dbo.EmployeeTerritories EmployeeTerritories 
       ON (EmployeeTerritories.EmployeeID = Employees.EmployeeID)) 
      INNER JOIN 
      Northwind.Dbo.Shippers Shippers 
      ON (Orders.ShipVia = Shippers.ShipperID)) 
     INNER JOIN 
     Northwind.Dbo.Suppliers Suppliers 
     ON (Products.SupplierID = Suppliers.SupplierID)) as ResultSet 
+1

問題/問題/錯誤是什麼? – McNets

+0

發現錯誤:'ResultID'列多次指定'EmployeeID'列 –

+0

您不需要'(')' - 內部連接不受操作順序影響 – Hogan

回答

0

更改部分Employees.EmployeeID as ID

1

僱員在SELECT語句中出現兩次,使用別名或刪除您的一列。

SELECT 
    Employees.EmployeeID <------- 
    ,Employees.LastName 
    ,Employees.FirstName 
    ,Employees.Title 
    ,Employees.TitleOfCourtesy 
    ,Employees.BirthDate 
    ,Employees.HireDate 
    ,Employees.Address 
    ,Employees.City 
    ,Employees.Region 
    ,Employees.PostalCode 
    ,Employees.Country 
    ,Employees.HomePhone 
    ,Employees.Extension 
    ,Employees.Photo 
    ,Employees.Notes 
    ,Employees.ReportsTo 
    ,Employees.PhotoPath 
    ,Orders.OrderID 
    ,Orders.CustomerID 
    ,Orders.EmployeeID  <------- 

例如:

,Orders.EmployeeID as OrderEmployeeID 
1

你的臨時表中的列名都將是單純的列名。它不會包含你的表名。要回答您的問題,請從您的選擇語句中刪除重複的列名或提供別名。

SELECT 
    Employees.EmployeeID // this is enough 
    ,Employees.LastName 
    ,Employees.FirstName 
    ,Employees.Title 
    ,Employees.TitleOfCourtesy 
    ,Employees.BirthDate 
    ,Employees.HireDate 
    ,Employees.Address 
    ,Employees.City 
    ,Employees.Region 
    ,Employees.PostalCode 
    ,Employees.Country 
    ,Employees.HomePhone 
    ,Employees.Extension 
    ,Employees.Photo 
    ,Employees.Notes 
    ,Employees.ReportsTo 
    ,Employees.PhotoPath 
    ,Orders.OrderID 
    ,Orders.CustomerID 
    // remove this line. Or use alias 
    ,Orders.EmployeeID AS [Orders_EmloyeeID] 
    // ,Orders.EmployeeID 
    ,Orders.OrderDate 
    ,Orders.RequiredDate 
    ,Orders.ShippedDate 
    ,Orders.ShipVia 
相關問題