0

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

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
4

3 回答 3

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)
于 2010-07-12T07:47:00.110 回答
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)
于 2010-07-12T07:48:21.030 回答
0

你不能用普通的 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 表上的索引。

于 2010-07-12T07:53:16.197 回答