我有一个在Microsoft SQL Server 2012上创建的数据库,我有两个表“dbo.Products”和“dbo.Categories”。我试图查询这两个表来为星型模式创建一个PRODUCT维度表。SQL INSERT INTO FROM SELECT
Products {ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued}
Categories {CategoryID, CategoryName, Description}
这两个表是从Northwind数据库
产品维度包含product_key,的productID,产品名称,的categoryID,这是从Northwind产品/分类表查询类别名称和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;
那么你的问题是什么? –
您需要在您的select中使用两个部分名称来解决名称歧义问题,我可以看到一个列名肯定是“CategoryID”,可能还有更多。 –
我的问题与SELECT语句中的语法有关,我尝试使用[dbo]。[Products]使用两个部分名称,但我得到“无效的对象名称”。 –