如何在 sql 服务器中创建临时 table,结果集为多个加入 table

how to create temp table in sql server with result set of multiple joined table

例如-我想在 sql 服务器中创建临时 table 并使用此查询的结果,因为我已尝试在下面进行查询但是,我发现 '列 'EmployeeID' 被多次指定为 'ResultSet` 请 help.thanks

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 

EmployeeID 在您的 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

更改部分Employees.EmployeeID as ID

您的临时 table 列名将只是列名。它不会包括您的 table 姓名。要回答您的问题,请从您的 select 语句中删除重复的列名称或提供别名。

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