How to get a trigger copy data from one field to another on same table?

I'm trying to create a simple trigger to copy data from one field (customer_id) to another field (custid)  in the same table (customers) on insert..
I've got to this but it doesn't work...

create trigger `fill_custid` AFTER INSERT on `customers`
for each row BEGIN
declare custid varchar(10);
declare customer_id int(7);
set custid = customer_id;
END;

Any ideas what's wrong??
inajamAsked:
Who is Participating?
 
Steve BinkConnect With a Mentor Commented:
After some experimentation, I have to say can't be done.  The docs specify that OLD and NEW are only available in BEFORE triggers.  For an auto-increment column, the value of NEW.fieldname is 0, not the auto-increment value you would expect.  

I was trying to find a workaround using LAST_INSERT_ID() or a similar trick, but there is no way to guarantee that the return is what you think you it is (other queries can affect that return).  

Looking at the other side of this question, why is it you need to duplicate the data?
0
 
Steve BinkCommented:
You are declaring local variables, but never giving them values.  You don't need variables to complete this task - you need to reference the row:

create trigger `fill_custid` AFTER INSERT on `customers`
for each row BEGIN
set NEW.custid = NEW.customer_id;
END;
0
 
ursangelCommented:
If ypu wish to use th variables , here it goes.
But this is a simple task.
you can perform it without variables.

Create trigger tr_test  on Test1
AFTER INSERT
As begin
      declare @customer_id int;

      select @customer_id = Col1 from inserted
      update Test1 set Col4  = str(@customer_id) where Col1  = @customer_id

END;
You can replace Col1 and Col4 with your corresponding table's columns
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
inajamAuthor Commented:
Thanks routinet/ursangel; I thought it should be something simple like routinet suggests but I now get this error...
       Updating of NEW row is not allowed in after trigger
And if I set it to BEFORE the value is just 0.
Then if I take the NEW out of either field it goes back to ....
       Unknown system variable 'custid'
which is why I thought I had to declare the variable

Does it make any difference that my customer_id field is auto incremented or that I'm using SQLyog to send my queries?
0
 
inajamAuthor Commented:
Thanks.   Our Access programmer asked me to do this as he wants a VARCHAR field as well as the INT field, something to do with the way Access handles each.   Maybe I'll ask him to find a different solution at his end, but I'll keep this open for now in case anyone has some ideas.   I couldn't get ursangel's suggestion to work either but maybe there is a longer winded way to execute this.
0
 
Steve BinkCommented:
Tell your Access programmer that Access, Visual Basic, and VBA can all convert strings to number and vice versa, and have been capable of doing so for quite some time.  Instead of him having you waste your time (2 days now, yes?) trying to do something that isn't recommended, he could have spent the FIVE MINUTES it would have taken to do the conversion in Access' SQL statement or VBA.  And that is presuming he actually needs to re-type the data instead of just using a formatting fix.... Access and VBA 'auto' type data and convert as necessary on the fly.

You might want to consider a more experienced programmer.
0
 
inajamAuthor Commented:
Appreciate the feedback routinet.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.