Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

trigger Expand / Collapse
Author
Message
Posted Tuesday, October 30, 2012 7:28 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 4:37 AM
Points: 746, Visits: 1,058
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
Post #1378766
Posted Tuesday, October 30, 2012 7:35 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, May 30, 2014 8:15 AM
Points: 1,056, Visits: 2,687
if (contribuintes.NUMEROPOSTO) ='CENTRAL'


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



Regards
Durai Nagarajan
Post #1378770
Posted Tuesday, October 30, 2012 7:40 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 4:37 AM
Points: 746, Visits: 1,058
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'.
Post #1378775
Posted Tuesday, October 30, 2012 7:47 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 13,005, Visits: 12,420
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 Moden's 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)
Post #1378781
Posted Tuesday, October 30, 2012 8:03 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 4:37 AM
Points: 746, Visits: 1,058
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
Post #1378790
Posted Tuesday, October 30, 2012 8:10 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 13,005, Visits: 12,420
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 Moden's 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)
Post #1378794
Posted Tuesday, October 30, 2012 8:13 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 31, 2014 3:05 PM
Points: 175, Visits: 449
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
Post #1378795
Posted Tuesday, October 30, 2012 8:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 13,005, Visits: 12,420
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 Moden's 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)
Post #1378798
Posted Tuesday, October 30, 2012 8:32 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 4:37 AM
Points: 746, Visits: 1,058
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
Post #1378801
Posted Tuesday, October 30, 2012 8:38 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:51 PM
Points: 13,005, Visits: 12,420
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 Moden's 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)
Post #1378806
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse