About triggers?

xiaoxiao2021-03-06  62

This is like this, now I have such a problem,

I want to establish such a trigger in SQL Server2000.

Table 1: table 2: B

Table 1 There is such a field: MNAME, MMONEY (int NT NULL, INT)

There is such a field in Table 2: GNAME, GF (int, int)

I hope to establish a trigger on the table two when inserting a field in the Table II.

Update the table one: Mname = gname of MName = gname in the table 2 will be inserted into the value of MName = gname

Automatically add a value after the GF field of the table is used. How to build this trigger.

Thank you!!!

-------------------------------------------------- -------------

Create Trigger UTG_TEST

On Tableb

For Insert, Update

AS

Update Tablea Set GF = (Select Mmoney from Tableb Where Tableb.mname = Tablea.gname)

Go

-------------------------------------------------- -------------

Create Trigger TR_IN ON [DBO]. [B]

For insert

AS

Update a set a.mmoney = a.mmoney ISNULL (Select Sum (GF) from Inserted M where m.gname = a.mname), 0)

-------------------------------------------------- -------------

Very good!

Do a trigger for the INSERT of B.

Get @ gname, @gf (corresponding column value) from the Inserted Temporary Table

Update a set mmoney = mmoney gf where mname = gname

-------------------------------------------------- -------------

It seems that there is no writing:

Create Trigger TR_IN ON [DBO]. [B]

For insert

AS

Update a

Set mmoney = a.mmoney m.gf

From inserted M

WHERE M.GNAME = a.mname

-------------------------------------------------- -------------

TO 9CBSM:

I Think your trigger has some troube.

IF USE SUM, There Must Be Some Error When Table B Update with Insert from A Subselect. Or Another Word, WHEN B Update Use as The Following Your Trigger CAN NOT Right Result.

INSERT B (GNAME, GF) (SELECT 1, 3 Union Select 1, 5)

-------------------------------------------------- -------------

OK!

Create Trigger TR_IN ON [DBO]. [B]

For insert

AS

Update a

Set mmoney = a.mmoney m.gf

From (SELECT GNAME, SUM (GF) AS GF from Inserted Group by gname) mwhere m.gname = a.mname

Your trigger results should be right, but each time you insert update the entire A table, the speed may not be accepted.

转载请注明原文地址:https://www.9cbs.com/read-90791.html

New Post(0)