我在Microsoft SQL Server 2012上创建了一个数据库,我有两个表“dbo.Products”和“dbo.Categories”。我正在尝试查询这两个表,以便为星型模式创建PRODUCT维度表。
Products {ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued}
Categories {CategoryID, CategoryName, Description}
这两个表来自Northwind数据库
Product Dimension包含从Northwind产品/类别表中查询的product_key,productID,productName,categoryID,categoryName和categoryDescription。
我正在使用的SQL代码在这里:
/*Create Dimension Table */
CREATE TABLE [dbo].[PRODUCT] (
[product_key] [int] IDENTITY (1, 1) NOT NULL Primary Key,
[productID] [int] NULL ,
[productName] nvarchar(30) NULL ,
[categoryID] [int] NULL ,
[categoryName] nvarchar(30) NULL ,
[categoryDescription] nvarchar(30) NULL
)
/* Populate (insert) Dimension Table with a select statement that joins
Product and Categories. */
INSERT INTO [dbo].[PRODUCT] (productID, productName, categoryID, categoryName, categoryDescription)
SELECT
ProductID, ProductName, CategoryID, CategoryName, CategoryDescription
FROM
Products
INNER JOIN
Categories ON Categories.CategoryID = Products.CategoryID;
答案 0 :(得分:1)
试试这个
INSERT INTO [acs4904_a1].[dbo].[PRODUCT]
(productID, productName, categoryID, categoryName, categoryDescription)
SELECT
P.ProductID,
P.ProductName,
C.CategoryID,
C.CategoryName,
C.CategoryDescription
FROM [acs4904_a1].[dbo].Products P
INNER JOIN [acs4904_a1].[dbo].Categories C ON P.CategoryID=C.CategoryID;
答案 1 :(得分:0)
您必须确定共享列的范围。
INSERT INTO [PRODUCT] (productID, productName, categoryID, categoryName, categoryDescription)
SELECT ProductID, ProductName, Products.CategoryID, CategoryName, CategoryDescription
FROM Products
INNER JOIN Categories ON Categories.CategoryID=Products.CategoryID;