2010-07-12 2 views
0

Bon, j'ai donc ce problème avec ma table de faits. J'ai besoin qu'il soit automatiquement rempli quand une nouvelle donnée est entrée sur toutes les autres tables dans la base de données qui a une clé étrangère sur ma table de faits. Dans ma procédure stockée, je compilé toutes les instructions d'insertion que j'ai et à la fin, car je veux aussi mettre à jour ma table de faits, je place cette requête:Requête d'insertion SQL Server sur le tableau de faits

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) 

Cependant, je reçois une erreur avec le message suivant :

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

Quelqu'un peut-il m'aider s'il vous plaît? Merci beaucoup. :)

MA PROCEDURE COMPLETE:

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 

Répondre

0

En supposant que les requêtes sous peuvent retourner chaque seul enregistrement cela devrait fonctionner

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) 
+0

Monsieur, merci pour cela. Mais j'ai essayé d'exécuter cette procédure stockée. Mais chaque fois que je lance mon application, cela me donne une erreur en disant que ma procédure attend un paramètre @ExpenseID qui n'a pas été fourni. Je vais modifier ma question pour mettre ma procédure stockée maintenant. Merci. – Smiley

+0

merci. Je l'ai eu fonctionne maintenant :) – Smiley

0

Changer votre requête à ceci:

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) 
+0

Cette requête me donne les messages d'erreur suivants: Msg 102, niveau 15, état 1, procédure ExpenseListInsert, ligne 38 Syntaxe incorrecte proche de ','. Msg 102, niveau 15, état 1, procédure ExpenseListInsert, ligne 39 Syntaxe incorrecte proche de ','. – Smiley

+0

@Kim Rivera - fixe. Je pensais que vous avez mis entre chaque 'SELECT' – cjk

0

Vous ne pouvez pas faire de sous-requêtes avec un INSERT à la vanille, vous devez faire un 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) 

Je regarde aussi le plan d'exécution pour que d'éventuels goulots d'étranglement de performance, et regarde aussi les indices sur les dépenses, DateTime et tables d'inventaire.

+0

Je ne comprends pas ce que cette requête fait. Pouvez-vous m'expliquer s'il vous plaît? Merci. :) – Smiley

+0

Lorsque vous utilisez VALUES avec INSERT, vous devez transmettre des valeurs SCALAR. Lorsque vous utilisez SELECT avec INSERT, vous pouvez transmettre un jeu de résultats tabulaire, dans ce cas, les résultats de vos trois sous-requêtes. –

+0

Cela peut sembler stupide mais je ne comprends toujours pas. Ce que je voudrais vraiment faire est ceci: INSERT INTO Fait (A, B, C, D) VALEURS (A, B devrait provenir d'une autre table, C devrait provenir d'une autre table, D devrait provenir d'une autre table) Est-ce que SELECT INSERT est applicable à cette situation? Merci beaucoup. – Smiley