SQL Server 在事实表上插入查询

发布于 2024-09-09 01:20:32 字数 2629 浏览 5 评论 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-09-16 01:20:32

假设每个子查询只能返回一条记录,这应该可行

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)

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)
ぃ双果 2024-09-16 01:20:32

您不能使用普通 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 上的索引和库存表。

You can't do subqueries with a vanilla INSERT, you need to do an 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)

I'd also look at the execution plan for that for possible performance bottle necks, and also look at the indexes on the Expense, DateTime and Inventory tables.

温柔一刀 2024-09-16 01:20:32

将您的查询更改为:

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)

Change your query to be this:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文