?
Solved

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

Posted on 2007-10-11
7
Medium Priority
?
2,285 Views
Last Modified: 2011-09-20
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??
0
Comment
Question by:inajam
  • 3
  • 3
7 Comments
 
LVL 51

Expert Comment

by:Steve Bink
ID: 20063196
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
 
LVL 5

Expert Comment

by:ursangel
ID: 20063330
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
 

Author Comment

by:inajam
ID: 20063414
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 51

Accepted Solution

by:
Steve Bink earned 1500 total points
ID: 20071862
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
 

Author Comment

by:inajam
ID: 20072206
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
 
LVL 51

Expert Comment

by:Steve Bink
ID: 20074219
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
 

Author Comment

by:inajam
ID: 20120696
Appreciate the feedback routinet.  
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question