Help defining variables in Mysql code

Running MySql on an HP ML350 G5 server.  Operating system is Small Business Server 2008 .

 I have written some SQL server stored programs.  I am not an expert.

I have now switched to using MySql.

What I want to do is quite simple. I am not sure what MySql calls a stored program.  
I want to write a routine which will read a row from a table.
I want to put the fields into variables.
Then I will calculate the values I want and write them to another table.

I know how to write an INSERT and UPDATE sql statements.

My question:
I need to know how to declare variables in a Mysql type of stored program.
May be someone could share some code showing how they declared variables, then created an INSERT statement
with the variables in the INSERT statement?
donpickAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
It is similar select (@variable:=column),... From tBle.

Here is the MySQL reference covering the different types of variable assignment.

http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

The version in the example is older, but I think the section exists for the version you use.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZberteocCommented:
You may not need any variable just do an update on the join of the 2 tables using the columns from the source table to compute the values you need to store in the target table:

update trg set
    col1 =(src.col1+src.col2)/src.col3
from
    tbl_source as src
    inner join tbl_target as trg
         on <join_clause_cond>
where
    <where_clause_cond>


If you only insert then select from the source table and insert into target table:

insert into tbl_target (col1)
select
    (src.col1+src.col2)/src.col3
from
    tbl_source
where
    <where_clause_cond>
0
donpickAuthor Commented:
Both of you:  Thank you for the prompt answers.  I pay for this service so I appreciate promptness.
Arnold:  Thank you for the link.

Hello Zberteoc:   Great idea.  I don't write much sql code so your ideas are very useful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.