Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I would like to know if anyone has any idea on how to achieve the following:

Say i do the following:

SELECT User.Name, User.Surname, User.phone FROM User WHERE User.ID = 1

Now say i want to store the fields returned above to user defined variables.
example:

SET @Name := User.Name
SET @Phone := User.Phone

How do i do this??

And how to i do an if statement in mysql.
Example:

If @Name = John THEN INSERT INTO ADMINTABLE() VALUES()
ELSE IF @Name = Paul THEN INSERT INTO SUPERISOR() VALUES()

How do i achieve the above?

And is there a way to iterate thru each row returned and the run functions against results?

Please someone help with any of the above

Thanks in advance
Posted
Comments
Zoltán Zörgő 25-Sep-12 15:58pm    
You want this in a stored procedure?
Ashy-G 25-Sep-12 16:57pm    
well doesn't matter if its in a stored procedure or a trigger or a function. Just need to know how you read a row from a table and then store each of field data into user defined variables. Thanks

1 solution

Hi Ashy

I think you can set a variable inside select statement like this:

SELECT
@Name := User.Name, @Phone := User.phone FROM User WHERE User.ID = 1


check this out http://dev.mysql.com/doc/refman/5.0/en/user-variables.html[^]

To get familiar with mysql if else statement please read this:

http://dev.mysql.com/doc/refman/5.0/en/if.html[^]

Regards
Pawan

Please mark the answer as solution if it fulfills your criteria.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900