SQL Server 在事实表上插入查询
好的,我的事实表有这个问题。当在数据库中的所有其他表上输入新数据(在我的事实表上有外键)时,我需要自动填充它。在我的存储过程中,我编译了所有的插入语句,最后,因为我也想更新我的事实表,所以我放置了这个查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设每个子查询只能返回一条记录,这应该可行
Assuming that the sub queries can each return only one record this should work
您不能使用普通 INSERT 执行子查询,您需要执行 INSERT...SELECT:
我还会查看执行计划以了解可能的性能瓶颈,并查看 Expense、DateTime 上的索引和库存表。
You can't do subqueries with a vanilla INSERT, you need to do an INSERT...SELECT:
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.
将您的查询更改为:
Change your query to be this: