Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
UDF params, return values & data conversion
Message
De
19/04/2004 07:55:22
 
 
À
19/04/2004 04:42:19
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00891543
Message ID:
00896062
Vues:
14
>Daniel,
>
>I'm using the function to calculate some totals from other tables and storing the result in a calculated field.
>
>Ideally this should probably be done by using an insert,update trigger, but I'm a bit of a novice here and can't figure out how to pass parameters to the trigger.
>
>In reality it's a bit more complex, but this does illustrate what I'd like to do:
>
>
>Example:
>Table1 Key Reservation_No,CBLineId,AmountTotal
>
>Table2 Key Reservation_No, CBLineId, LineId, Amount
>
>
>On insert and update in Table2 the AmountTotal of the corresponding record in Table1 should be updated with SUM(Amount) where Table1.Reservation_No = Table2.Reservation_No AND Tale1.CBLine = Table2.CBLine
>
>

Using calculated fields instead of triggers is a design question; both will accomplish the same thing, but your either pay the piper on Table2 update (via a trigger), or when you query Table1 (via the calculated field).

To use the former, you don't pass parameters, you query what is in the Inserted table while in your trigger. So, a simple insert trigger for Table2 would be
declare @reservation_no {type}, @cblineid {type}
select @reservation_no = reservation_no, @cblineid = cblineid
   from inserted

declare @amount = {type}
select @amount = sum(amount) from table2
   where @reservation_no = reservation_no, @cblineid = cblineid

update table1
   set amounttotal = @amount 
where @reservation_no = reservation_no, @cblineid = cblineid
Of course, there are some pitfalls here:
1) for performance, you may only want the trigger to fire if any of the three values involved were changed
2) If for some reason the trigger does not properly complete, you will have invalid data

The alternative is to use a calculated field and call a UDF, like you are trying to do. Most likely, you will have to create a specific UDF for each table you want to put a calculated field in.

Hope that helps.
set amounttotal =
Dan LeClair
www.cyberwombat.com
SET RANT ON - The Wombat Blog

Life isn’t a morality contest and purity makes a poor shield. - J. Peter Mulhern
Disclaimer: The comments made here are only my OPINIONS on various aspects of VFP, SQL Server, VS.NET, systems development, or life in general, and my OPINIONS should not be construed to be the authoritative word on any subject. No warranties or degrees of veracity are expressed or implied. Void where prohibited. Side effects may included dizziness, spontaneous combustion, or unexplainable cravings for dark beer. Wash with like colors only, serve immediately for best flavor.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform