[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Frank Dulk

3/24/2007 10:33:00 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?


4 Answers

Jens K. Suessmeyer

3/24/2007 11:13:00 PM

0

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.sqlser...
---


"Frank Dulk" <fdulk@bol.com.br> wrote in message
news:%2340SqTmbHHA.4476@TK2MSFTNGP03.phx.gbl...
> 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?
>
>

Frank Dulk

3/27/2007 12:53:00 AM

0

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...
> 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.sqlser...
> ---
>
>
> "Frank Dulk" <fdulk@bol.com.br> wrote in message
> news:%2340SqTmbHHA.4476@TK2MSFTNGP03.phx.gbl...
>> 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?
>>
>>
>


Hugo Kornelis

3/27/2007 10:02:00 PM

0

On Mon, 26 Mar 2007 21:52:31 -0300, Frank Dulk wrote:

>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'.

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
My SQL Server blog: http://sqlblog.com/blogs/hug...

Frank Dulk

3/31/2007 10:01:00 PM

0

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...
> On Mon, 26 Mar 2007 21:52:31 -0300, Frank Dulk wrote:
>
>>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'.
>
> 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
> My SQL Server blog: http://sqlblog.com/blogs/hug...