SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


trigger


trigger

Author
Message
river1
river1
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4372 Visits: 1366
Hi guys,

I have created this trigger:

create trigger TRG_CONTRIBUINTES_VERIFCA_RF_E_NIF
on contribuintes
for update
as


if (contribuintes.NUMEROPOSTO) ='CENTRAL'

BEGIN


if contribuintes.cod_rep_fiscal not in
(select codigo from V_RETURN_RF)
begin

update
contribuintes
set cod_rep_fiscal = b.cod_rep_fiscal,
nif =b.nif,
filial_number = b.filial_number
from
deleted b
where
nif_antigo = b.nif_antigo

end

else

if contribuintes.nif <> deleted.nif

begin
update
contribuintes
set nif = a.nif,
filial_number =b.filial_number

from
deleted a
where
nif_antigo = a.nif_antigo

end


END



When I parse the code it works fine, but when I run the code it gives me the following error:

Msg 4104, Level 16, State 1, Procedure TRG_CONTRIBUINTES_VERIFCA_RF_E_NIF, Line 11
The multi-part identifier "contribuintes.NUMEROPOSTO" could not be bound.
Msg 4104, Level 16, State 1, Procedure TRG_CONTRIBUINTES_VERIFCA_RF_E_NIF, Line 16
The multi-part identifier "contribuintes.cod_rep_fiscal" could not be bound.
Msg 209, Level 16, State 1, Procedure TRG_CONTRIBUINTES_VERIFCA_RF_E_NIF, Line 28
Ambiguous column name 'nif_antigo'.


Can you please help?

thanks
durai nagarajan
durai nagarajan
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3441 Visits: 2784
if (contribuintes.NUMEROPOSTO) ='CENTRAL'


try using inserted or deleted table within the trigger to populate the neccesary flow

Regards
Durai Nagarajan
river1
river1
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4372 Visits: 1366
Thank you for the reply.

Same problem.

Error bellow:

Msg 4104, Level 16, State 1, Procedure TRG_CONTRIBUINTES_VERIFCA_RF_E_NIF, Line 11
The multi-part identifier "deleted.NUMEROPOSTO" could not be bound.
Msg 4104, Level 16, State 1, Procedure TRG_CONTRIBUINTES_VERIFCA_RF_E_NIF, Line 16
The multi-part identifier "contribuintes.cod_rep_fiscal" could not be bound.
Msg 209, Level 16, State 1, Procedure TRG_CONTRIBUINTES_VERIFCA_RF_E_NIF, Line 28
Ambiguous column name 'nif_antigo'.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61669 Visits: 17954
You have some very serious logic flaws in here. You keep trying to reference your table like a variable and it just doesn't work like that.


if (contribuintes.NUMEROPOSTO) ='CENTRAL'

if contribuintes.cod_rep_fiscal not in

if contribuintes.nif <> deleted.nif



You have certainly been around here long enough to know that we need ddl and some sample data along with desired results. I am not sure exactly what you are trying to do in this trigger. It seems that maybe you want to use MERGE here...once you post the details we can take a look.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
river1
river1
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4372 Visits: 1366
Hi,

What I'm trying to do is:

I have a table named "contribuintes" this table has several fields, including the fileds:

numeroposto, cod_rep_fiscal and NIF.

I want that, if an update takes place and the value of the column numeroposto is updated to "Central", then

The trigger sees if the "NIF" value was changed.
If it was changed, then it needs to get back to the same value it had before the update.

At the same time, if the column "cod_rep_fiscal" was changed and the new value does not exists in the view V_RETURN_RF then it needs to be chamged again to it's original value.


Thanks
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61669 Visits: 17954
Sean Lange (10/30/2012)

You have certainly been around here long enough to know that we need ddl and some sample data along with desired results.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
matt.newman
matt.newman
SSC Eights!
SSC Eights! (832 reputation)SSC Eights! (832 reputation)SSC Eights! (832 reputation)SSC Eights! (832 reputation)SSC Eights! (832 reputation)SSC Eights! (832 reputation)SSC Eights! (832 reputation)SSC Eights! (832 reputation)

Group: General Forum Members
Points: 832 Visits: 473
Take another try at your update statements.

Update ref set column = value
from
table ref
join othertable ref2 on ref.column = ref2.column

First google result I had found:
http://geekswithblogs.net/faizanahmad/archive/2009/01/05/join-in-sql-update--statement.aspx
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61669 Visits: 17954
Incredibly sparse on details but I think this is close...


update contribuintes
set nif = c.nif
from contribuintes c
join inserted i on i.PrimaryKey = c.PrimaryKey
where i.numeroposto = 'Central'

update contribuintes
set cod_rep_fiscal = c.cod_rep_fiscal
from contribuintes c
join inserted i on i.PrimaryKey = c.PrimaryKey
where i.cod_rep_fiscal not in (select codigo from V_RETURN_RF)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
river1
river1
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4372 Visits: 1366
I think that is what is needed but we have a problem:

I only want that the trigger acts if the column numeroposto new value is 'Central'. If it's not, it shoud not do any thing
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61669 Visits: 17954
river1 (10/30/2012)
I think that is what is needed but we have a problem:

I only want that the trigger acts if the column numeroposto new value is 'Central'. If it's not, it shoud not do any thing


Two things. The trigger will always fire on every update. You need to break your mindset that there is a single new value. It would be easy enough to add the additional where clause to the second update if that is also needed.


and i.numeroposto = 'Central'



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search