好的,所以我的事实表有这个问题。当在我的事实表上有外键的数据库中的所有其他表上输入新数据时,我需要自动填充它。在我的存储过程中,我编译了我拥有的所有插入语句,最后,因为我还想更新我的事实表,所以我放置了这个查询:
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