October 30, 2012 at 7:28 am
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
October 30, 2012 at 7:35 am
if (contribuintes.NUMEROPOSTO) ='CENTRAL'
try using inserted or deleted table within the trigger to populate the neccesary flow
Regards
Durai Nagarajan
October 30, 2012 at 7:40 am
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'.
October 30, 2012 at 7:47 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 30, 2012 at 8:03 am
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
October 30, 2012 at 8:10 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 30, 2012 at 8:13 am
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
October 30, 2012 at 8:23 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 30, 2012 at 8:32 am
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
October 30, 2012 at 8:38 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 30, 2012 at 8:46 am
update c
set c.nif = (needs reference or is ambiguous).nif
from contribuintes c
join...etc...
Either the above with reference, or just take out the c and preface with full name. Sorry for little detail, just getting up and running this morning.
October 30, 2012 at 8:50 am
The code:
update
contribuintes
set
nif = c.nif
from
contribuintes c
join
inserted i
on
i.nif_antigo = c.nif_antigo
where
i.numeroposto = 'Central'
Has a problem.
I want that If the value from NIF is changed, then it needs to get back to the original value again.
So, why use the inserted instead of the deleted table?
October 30, 2012 at 8:52 am
like this:
update
contribuintes
set
nif = c.nif
from
contribuintes c
join
deleted d
on
d.nif_antigo = c.nif_antigo
where
c.numeroposto = 'Central'
October 30, 2012 at 8:55 am
river1 (10/30/2012)
like this:update
contribuintes
set
nif = c.nif
from
contribuintes c
join
deleted d
on
d.nif_antigo = c.nif_antigo
where
c.numeroposto = 'Central'
Think you need to set this to d.nif and not c.nif.
_______________________________________________________________
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 30, 2012 at 8:56 am
Sorry, trying to do a few things but overall
declare @insertednif and @deletednif
select via select
if insertednif <> deletednif
begin
rollback
return
end
Viewing 15 posts - 1 through 15 (of 39 total)
You must be logged in to reply to this topic. Login to reply