好的,所以我的Fact Table有这个问题。我需要它在数据库中所有其他表上输入新数据时自动填充,该表在我的Fact Table上有一个外键。在我的存储过程中,我编译了所有插入语句,最后编译,因为我还想更新我的Fact Table,我放置了这个查询:
INSERT INTO Fact (AccountID, ExpenseID, DateTimeID, InventoryID)
VALUES (@AccountID,
(SELECT ExpenseID FROM Expenses WHERE WaterBill = @WaterBill AND ElectricBill = @ElectricBill AND OfficeRent = @OfficeRent,
SELECT DateTimeID FROM DateTime WHERE MonthNo = @MonthNo AND Date = @Date AND Year = @Year AND Time = @Time AND Day = @Day AND DayNo = @DayNo,
SELECT InventoryID FROM Inventory WHERE ProductInID = @ProductInID AND ProductOutID = @ProductOutID)
但是,我收到以下消息的错误:
Subqueries are not allowed in this context. Only scalar expressions are allowed.
有人可以帮帮我吗?非常感谢。 :)
我的完整程序:
ALTER PROCEDURE [dbo].[ExpenseListInsert]
@AccountID char(6),
@ExpenseID int,
@DateTimeID int,
@InventoryID int,
@WaterBill decimal(19, 4),
@ElectricBill decimal(19, 4),
@OfficeRent decimal(19, 4),
@Miscellaneous decimal(19, 4),
@ProductsExpense decimal(19, 4),
@Subtotal decimal(19, 4),
@ProductInID int,
@ProductOutID int,
@Product30001 int,
@Product30002 int,
@Product30003 int,
@MonthNo int,
@Date int,
@Year int,
@Time char(11),
@Day char(10),
@DayNo int
AS
INSERT INTO Expenses (WaterBill, ElectricBill, OfficeRent, Miscellaneous, ProductsExpense, Subtotal)
VALUES(@WaterBill, @ElectricBill, @OfficeRent, @Miscellaneous, @ProductsExpense, @Subtotal)
INSERT INTO ProductIn (ProductInID, Product30001, Product30002, Product30003)
VALUES(@ProductInID, @Product30001, @Product30002, @Product30003)
INSERT INTO ProductOut (ProductOutID, Product30001, Product30002, Product30003)
VALUES(@ProductOutID, '0', '0', '0')
INSERT INTO Inventory (ProductInID, ProductOutID)
VALUES (@ProductInID, @ProductOutID)
INSERT INTO DateTime (MonthNo, Date, Year, Time, Day, DayNo)
VALUES (@MonthNo, @Date, @Year, @Time, @Day, @DayNo)
SELECT @ExpenseID = ExpenseID FROM Expenses WHERE WaterBill = @WaterBill AND ElectricBill = @ElectricBill AND OfficeRent = @OfficeRent
SELECT @DateTimeID = DateTimeID FROM DateTime WHERE MonthNo = @MonthNo AND Date = @Date AND Year = @Year AND Time = @Time AND Day = @Day AND DayNo = @DayNo
SELECT @InventoryID = InventoryID FROM Inventory WHERE ProductInID = @ProductInID AND ProductOutID = @ProductOutID
INSERT INTO Fact (AccountID, ExpenseID, DateTimeID, InventoryID)
VALUES (@AccountID, @ExpenseID, @DateTimeID, @InventoryID)
RETURN
答案 0 :(得分:0)
将您的查询更改为:
INSERT INTO Fact (AccountID, ExpenseID, DateTimeID, InventoryID)
SELECT @AccountID,
(SELECT ExpenseID FROM Expenses WHERE WaterBill = @WaterBill AND ElectricBill = @ElectricBill AND OfficeRent = @OfficeRent),
(SELECT DateTimeID FROM DateTime WHERE MonthNo = @MonthNo AND Date = @Date AND Year = @Year AND Time = @Time AND Day = @Day AND DayNo = @DayNo),
(SELECT InventoryID FROM Inventory WHERE ProductInID = @ProductInID AND ProductOutID = @ProductOutID)
答案 1 :(得分:0)
假设子查询每个只返回一条记录,这应该有效
DECLARE @ExpenseID int, @DateTimeID int, @InventoryID int
SELECT @ExpenseID= ExpenseID FROM Expenses WHERE WaterBill = @WaterBill AND ElectricBill = @ElectricBill AND OfficeRent = @OfficeRent
SELECT @DateTimeID = DateTimeID FROM DateTime WHERE MonthNo = @MonthNo AND Date = @Date AND Year = @Year AND Time = @Time AND Day = @Day AND DayNo = @DayNo
SELECT @InventoryID = InventoryID FROM Inventory WHERE ProductInID = @ProductInID AND ProductOutID = @ProductOutID
INSERT INTO Fact (AccountID, ExpenseID, DateTimeID, InventoryID)
VALUES (@AccountID, @ExpenseID, @DateTimeID, @InventoryID)
答案 2 :(得分:0)
您无法使用vanilla INSERT执行子查询,您需要执行INSERT ... SELECT:
INSERT INTO [Fact] (AccountID, ExpenseID, DateTimeID, InventoryID)
SELECT @AccountID,
(SELECT ExpenseID FROM [Expenses] WHERE WaterBill = @WaterBill AND ElectricBill = @ElectricBill AND OfficeRent = @OfficeRent),
(SELECT DateTimeID FROM [DateTime] WHERE MonthNo = @MonthNo AND [Date] = @Date AND [Year] = @Year AND [Time] = @Time AND [Day] = @Day AND DayNo = @DayNo),
(SELECT InventoryID FROM Inventory WHERE ProductInID = @ProductInID AND ProductOutID = @ProductOutID)
我还会查看可能的性能瓶颈的执行计划,还可以查看Expense,DateTime和Inventory表中的索引。