Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Trigger resulting in NULL value
Message
De
11/07/2013 15:08:34
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Versions des environnements
SQL Server:
SQL Server 6.5 and older
Application:
Web
Divers
Thread ID:
01578216
Message ID:
01578220
Vues:
33
>There is one common mistake in this trigger as it's written to work only with a single insert.
>
>I'll attempt to fix your code:
>
>
>CREATE TRIGGER [dbo].[Transfers_Insert_AdjustItemQty]
>   ON [dbo].[transfers]
>   AFTER INSERT
>AS 
>BEGIN
>	SET NOCOUNT ON;
>
>                   MERGE dbo.Items as Target
>                  USING (select trn_itmfk as ItemId, SUM(trn_Qty) as TotalQty 
>FROM Inserted GROUP BY trn_itmfk) AS Source ON Target.ItemId = Source.ItemId
>
>                 WHEN Matched  AND Target.ItemTypId  = '43' THEN UPDATE
>                 SET iQtyOnHand = iQtyOnHand - COALESCE(Source.TotalQty,0);
>
>
>END
>
>
>GO
>
>This is assuming you're using SQL Server 2008 and up.

so if there is a statement with multiple inserts, the trigger will only run once? Not once per insert?

I n the meantime I had actually re-written it like this:
	UPDATE dbo.Items
			SET Items.iqtyonhand = Items.iqtyonhand - Inserted.trn_qty
		FROM dbo.Items
			INNER JOIN Inserted ON Items.iItemid = Inserted.trn_itmfk
		WHERE iItemTypId = '43'
Would that still have the same mistake?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform