Trigger + UpdLock

Trigger + UpdLock

Post by Ricardo Cost » Tue, 15 Jul 2003 20:41:27



Hi,

First of all sorry for this looong question...

I have two tables
1: CMENVI - with data changes
2: BAS_EMPRESA  - data table

On BAS_EMPRESA I have the following triggers on insert, update.

CREATE TRIGGER PSNI_BAS_EMPRESA ON BAS_EMPRESA
    FOR  INSERT, UPDATE
   AS



  IF (SYSTEM_USER NOT LIKE 'REPL%') BEGIN





     INSERT INTO CMENVI ( dCDA, iSEQ, iNCO, sTBA, sCLA, sDEL, sICE,


       IF UPDATE(ESNOME) BEGIN     INSERT INTO CMENVI ( dCDA, iSEQ,
iNCO, sTBA, sCLA, sDEL, sICE,    CT_char)    SELECT CONVERT(DATETIME,


       IF UPDATE(ESRAZAO) BEGIN     INSERT INTO CMENVI ( dCDA, iSEQ,
iNCO, sTBA, sCLA, sDEL, sICE,    CT_char)    SELECT CONVERT(DATETIME,



END

After updated record, it should insert the changed data into table
CMENVI, but only its primary key and the one changed. What this trigger
do ..
On column iseq goes the same number for all columns that have changed on
the same record. If all 3 columns were changed (for example) I must have
CMENVI like this if I update  registries.

Data        Seq.    Nr Cols.  Table      Column     --    --       Value

dCDA     -  iSEQ - iNCO -    sTBA     - sCLA     - sDEL - sICE - CT_CHAR
01/01/01 -    1      3    BAS_EMPRESA  - 'ESESTAB'  - 'N' -  'N' - '001'
01/01/01 -    1      3    BAS_EMPRESA  - 'ESNOME'  - 'N' -  'N' -  'T1'
01/01/01 -    1      3    BAS_EMPRESA  - 'ESRAZAO'  - 'N' - 'N' -  'TR1'

01/01/01 -    2      3    BAS_EMPRESA  - 'ESESTAB'  - 'N' -  'N' - '002'
01/01/01 -    2      3    BAS_EMPRESA  - 'ESNOME'  - 'N' -  'N' -  'T2'
01/01/01 -    2      3    BAS_EMPRESA  - 'ESRAZAO'  - 'N' - 'N' -  'TR2'

If I update registry by registry I have no problem, but if I do
something like Update BAS_EMRPESA set ESNOME = ESNOME,  to update all
registries I get:
Data        Seq.    Nr Cols.  Table      Column     --    --       Value

dCDA     -  iSEQ - iNCO -    sTBA     - sCLA     - sDEL - sICE - CT_CHAR
01/01/01 -    1      3    BAS_EMPRESA  - 'ESESTAB'  - 'N' -  'N' - '001'
01/01/01 -    1      3    BAS_EMPRESA  - 'ESNOME'  - 'N' -  'N' -  'T1'
01/01/01 -    1      3    BAS_EMPRESA  - 'ESRAZAO'  - 'N' - 'N' -  'TR1'

01/01/01 -    1      3    BAS_EMPRESA  - 'ESESTAB'  - 'N' -  'N' - '002'
01/01/01 -    1      3    BAS_EMPRESA  - 'ESNOME'  - 'N' -  'N' -  'T2'
01/01/01 -    1      3    BAS_EMPRESA  - 'ESRAZAO'  - 'N' - 'N' -  'TR2'

The two record with the same sequence.

Is there any way to do this? I'm getting bold here.. :)

Thanks in advance for any ligth!

Ricardo Costa
Florianpolis - SC - Brazil

 
 
 

Trigger + UpdLock

Post by Ricardo Cost » Wed, 16 Jul 2003 15:10:34


I saw that when the trigger inserts tha values to other table, first it
get all the first record, then the second and so on (If I make a
complete update in the table (UPDATE TABLE SET COL = COL, for example)
Is there a way to tell the trigger that it must first complete cicle and
then go on to the next one?

Thanks in advance

------------------------------
Ricardo Costa
IONICS Technology
Florianopolis - SC - Brasil

 
 
 

Trigger + UpdLock

Post by Stefan Gustafsso » Wed, 16 Jul 2003 15:36:04


The reason for your problem is that the trigger is only executed once even
when you update multiple rows.

When updating multiple rows, the INSERTED table will contain multiple rows.


same value for all inserted rows.

I think that you will have to create a cursor and loop through the inserted
table record by record.

/SG


 
 
 

Trigger + UpdLock

Post by Ricardo Cost » Wed, 16 Jul 2003 17:24:38



mean, I get this variable to insert records on other table.. Even so
this variable is on inserted table?


Thanks Stefan for the help

Ricardo Costa


> The reason for your problem is that the trigger is only executed once even
> when you update multiple rows.

> When updating multiple rows, the INSERTED table will contain multiple rows.


> same value for all inserted rows.

> I think that you will have to create a cursor and loop through the inserted
> table record by record.

> /SG




 
 
 

1. Deadlock Issue with UPDLOCK Hint

I am experiencing some problems with deadlocks and in a proc of mine. The
proc is used in a pseudo queuing mechanism to retrieve the next row to
process, and passes certain info back to the client as output params. The
client app is constantly looking for work, this proc gives it the details.
We are testing multiple client procs, but have started to get deadlocks. The
deadlock is between the two calls of this procedure, one from each client. I
would expect some blocking, but cannot workout why it is deadlocking

CREATE PROCEDURE spLPQuote_GetRecordToSend








AS

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE --REPEATABLE READ

SET NOCOUNT ON



BEGIN TRANSACTION




FROM LPQuote_Quote Q (UPDLOCK, ROWLOCK) INNER JOIN LPQuote_Product P
(READCOMMITTED) ON Q.ProductNum = P.ProductNum


AND ( Q.QuoteStatus = 'New' ))

AND (P.TxHostName = HOST_NAME() OR P.TxOverride = 1)

ORDER BY Q.QuoteStatus, Q.TimeOutSecs, Q.RequestDateTime


BEGIN





END

COMMIT TRANSACTION


GO

This problem looks like that described in KB Q179362, but this was a 6.0/6.5
KB, with no mention of 7.0. Either way the workarounds, use an index and/or
a where clause for the update are being met.

The execution plan shows a clustered index scan on the RequestDateTime
field, and one on the PK column (ProductNum) of the LPQuote_Product table.

Can anyone shed some light on this?

Thanks

--

Darren Green

2. DB2 & Solaris problems!!

3. Deadlocks while using updlock in COM+

4. Dynamically refer to ADO recordset

5. Need help: Locking hint (UPDLOCK)

6. UTIL_FILE.READ_ERROR

7. Use of updlock

8. Loading database from CD

9. UPDLOCK READPAST hints

10. Updlock and views in SQL server 7.0

11. Cannot specify UPDLOCK or TABLOCKX on a read-only table in a cursor

12. UpdLock to get only a record

13. updlock