Search code examples
sql-serverinsertfact-table

SQL Server Insert Query on Fact Table


Okay, so I have this problem with my Fact Table. I need it to automatically be filled when a new data is entered on all the other tables in the database which has a foreign key on my Fact Table. In my stored procedure, I compiled all the insert statements I have and at the end, since I also want to update my Fact Table, I place this query:

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)

However, I get an error with the following message:

Subqueries are not allowed in this context. Only scalar expressions are allowed.

Can anybody please help me? Thanks a lot. :)

MY COMPLETE PROCEDURE:

    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

Solution

  • Assuming that the sub queries can each return only one record this should work

    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)