Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass the value to a field in a trigger

Status
Not open for further replies.

williamkremer

Programmer
Oct 27, 2005
61
I think that I'm close here, but I'm getting errors, and what I am attempting to do is to pull that ch.accountno into the @accountno variable so that the update in the contact1 occurs where that accountno (unique) is.
****************************************
CREATE TRIGGER TrgLastCont ON Ch
FOR Update AS
declare @accountno varchar (20)
declare @ondate varchar (11)

if exists (SELECT top 1 cast (ch.ondate as varchar(11)), ch.accountno
from ch where (ch.ondate <> '' or ch.ondate is not null)
and ch.resultcode = 'CT' and ch.ondate = getdate()
order by ch.ondate desc)

Begin
set @accountno = ch.accountno
update Contact1 set Key3 = @ondate
where contact1.accountno = @accountno
end
***********************
The error reads: "Line 13: The column prefix 'ch' does not match with a table name or alias name used in the query." I think I need some learnin' here. Thanks in advance!

Best regards,
Bill
 
Get rid of the if exists and change the select:
Code:
SELECT top 1 cast (ch.ondate as varchar(11)), [b]@accountno = ch.accountno[/b] 
from ch where (ch.ondate <> '' or ch.ondate is not null) 
and ch.resultcode = 'CT' and ch.ondate = getdate()
order by ch.ondate desc)

Then

Code:
IF @accountno is not null then
Begin
 ... your code
End

Jim
 
Hmmmmm. The error now is "Incorrect syntax near '=' "
Also, it doesn't like my including the variable assignment inside of the select statement. Maybe I misunderstood:
***********************

SELECT top 1 cast (ch.ondate as varchar(11)), @accountno = ch.accountno
from ch where (ch.ondate <> '' or ch.ondate is not null)
and ch.resultcode = 'CT' and ch.ondate = getdate()
order by ch.ondate desc
IF @accountno is not null then
Begin
update Contact1 set Key3 = @ondate
where contact1.accountno = @accountno
end


Best regards,
Bill
 
Sorry, you need to remove the select of the ondate column:
Code:
SELECT top 1 @accountno = ch.accountno 
from ch 
where (ch.ondate <> '' or ch.ondate is not null) 
   and ch.resultcode = 'CT' and ch.ondate = getdate()
order by ch.ondate desc
 
I cannot remove it from the query because it is the value that I need moved to the other table. (where the accountno = the @accountno)

Best regards,
Bill
 
You don't need:
Code:
cast (ch.ondate as varchar(11))

You just need to assign the account number based on your criteria. Once you assign the value into the variable, you can use it in the following sql. There is no where in your code that you need ch.ondate other than your WHERE clause, so, there is no reason to SELECT it.

 
Yes it is! it's right here:
update Contact1 set Key3 = @ondate

Best regards,
Bill
 
ohh sorry I missed that @ondate variable. Then change your SELECT to this:

Code:
SELECT top 1 [b]@ondate = cast (ch.ondate as varchar(11)), @accountno = ch.accountno[/b]
from ch where (ch.ondate <> '' or ch.ondate is not null) 
and ch.resultcode = 'CT' and ch.ondate = getdate()
order by ch.ondate desc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top