Groups | Blog | Home
all groups > sql server programming > march 2007 >

sql server programming : trigger deleted


Frank Dulk
3/24/2007 7:32:38 PM
In my system I have a table that records the stock movements and another
that records the balance of current stock of the product. The triggers
update the stock balance every time that movement exists in the stock table.
The trigger is the following:

Create TRIGGER [DELETEESTOQUE] ON [EstoqueMovimento]
AFTER DELETE AS
BEGIN
DECLARE @EMPRESA [NUMERIC]
DECLARE @PRODUTO [NUMERIC]
DECLARE @ESTOQUE [MONEY]
SET @EMPRESA=(SELECT EMPRESA FROM DELETED)
SET @PRODUTO=(SELECT CODPRODUTO FROM DELETED)
SET @ESTOQUE=ISNULL((SELECT SUM(ENTRADA)-SUM(SAÍDA) FROM ESTOQUEMOVIMENTO
WHERE CODPRODUTO=@PRODUTO AND EMPRESA=@EMPRESA AND LEFT(ENTSAI,1)='X'),0)
SET @ESTOQUE=@ESTOQUE-ISNULL((SELECT SUM(Qtdade) FROM ESTOQUERESERVADO WHERE
CODPRODUTO=@PRODUTO AND EMPRESA=@EMPRESA),0)
UPDATE LOCALIZAPRODUTOS SET ESTOQUE=@ESTOQUE WHERE CODPRODUTO=@PRODUTO AND
CODEMPRESA=@EMPRESA
END

The problem is when more than a records was excluded, gives mistake in the
instruction SET @EMPRESA=(SELECT EMPRESA FROM DELETED).

How does to alter the trigger for her to execute him calculate registration
for registration of the table Deleted?

Jens K. Suessmeyer
3/25/2007 12:13:20 AM
Triggers are fired per statament and NOT per row, you will always have to
use a logic that will cover multi-rwo occurence.

So the unstested version should be something like:

UPDATE LOCALIZAPRODUTOS
SET ESTOQUE= SUB1.SUM1 - SUB2.SUM2
FROM LOCALIZAPRODUTOS L
INNER JOIN DELETED D ON
L.CODPRODUTO = D.CODPRODUTO
AND L.CODEMPRESA = D.EMPRESA
INNER JOIN
( SELECT ISNULL(SUM(ENTRADA)-SUM(SAÍDA),0) SUM2 FROM ESTOQUEMOVIMENTO
WHERE LEFT(ENTSAI,1)='X') SUB1
ON SUB1.CODPRODUTO = D.CODPRODUTO AND SUB1.EMPRESA=D.EMPRESA
INNER JOIN
(SELECT ISNULL(SUM(Qtdade),0) AS SUM2 FROM ESTOQUERESERVADO) SUB2
ON SUB2.CODPRODUTO = D.CODPRODUTO AND SUB1.EMPRESA=D.EMPRESA

Jens K Suessmeyer.

---
http://www.sqlserver2005.de
---


[quoted text, click to view]
Frank Dulk
3/26/2007 9:52:31 PM
The logic is really what you passed in the answer, but SQL didn't accept the
update. gave the following mistakes, that I didn't get to correct:

Server: Msg 207, Level 16, State 3, Procedure DELETEESTOQUE, Line 3
Invalid column name 'CODPRODUTO'.
Server: Msg 207, Level 16, State 1, Procedure DELETEESTOQUE, Line 3
Invalid column name 'EMPRESA'.
Server: Msg 207, Level 16, State 1, Procedure DELETEESTOQUE, Line 3
Invalid column name 'CODPRODUTO'.
Server: Msg 207, Level 16, State 1, Procedure DELETEESTOQUE, Line 3
Invalid column name 'EMPRESA'.

"Jens K. Suessmeyer" <Jens@NoSpamhere-sqlserver2005.de> escreveu na mensagem
news:OExMGombHHA.4832@TK2MSFTNGP02.phx.gbl...
[quoted text, click to view]

Hugo Kornelis
3/28/2007 12:02:01 AM
[quoted text, click to view]

Hi Frank,

That means that you have misspelled either the table or (more probable)
the column names.

Could you post the CREATE TABLE statement used to create the table and
the exact code of the trigger? Also, see www.aspfaq.com/5006.

--
Hugo Kornelis, SQL Server MVP
Frank Dulk
3/31/2007 7:01:29 PM
the names of the tables and fields are these same ones, observing the code
noticed that in Sub1 and Sub2 associations and filters exist in the fields
CodEmpresa and CodProduto, that actually didn't do part of Select of those
subs, I tried to modify for:

Create TRIGGER [DELETEESTOQUE] ON [EstoqueMovimento]
AFTER DELETE AS
UPDATE LOCALIZAPRODUTOS
SET ESTOQUE= SUB1.SUM1 - SUB2.SUM2 FROM LOCALIZAPRODUTOS L
INNER JOIN DELETED D ON L.CODPRODUTO = D.CODPRODUTO
AND L.CODEMPRESA = D.EMPRESA INNER JOIN
( SELECT Empresa, CodProduto, ISNULL(SUM(ENTRADA)-SUM(SAÍDA),0) SUM1 FROM
ESTOQUEMOVIMENTO
WHERE LEFT(ENTSAI,1)='X' Group By Empresa, Codproduto) SUB1
ON SUB1.CODPRODUTO = D.CODPRODUTO AND SUB1.EMPRESA=D.EMPRESA
INNER JOIN
(SELECT Empresa, CodProduto, ISNULL(SUM(Qtdade),0) AS SUM2 FROM
ESTOQUERESERVADO Group By Empresa, CodProduto) SUB2
ON SUB2.CODPRODUTO = D.CODPRODUTO AND SUB1.EMPRESA=D.EMPRESA

Including those fields in Select, and beauty, SQL accepted Trigger, but I
went to exclude registrations and, she doesn't make anything

turning a soap opera, I believed that had a much more practical way of
doing that.


"Hugo Kornelis" <hugo@perFact.REMOVETHIS.info.INVALID> escreveu na mensagem
news:7t4j039hbk5vu8j7pjv9g6d17sl50ui9ho@4ax.com...
[quoted text, click to view]

AddThis Social Bookmark Button