Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MySQL
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 25-Sep-12 9:46am
Ashy-G522
Comments
Zoltán Zörgő at 25-Sep-12 15:58pm
   
You want this in a stored procedure?
Ashy-G at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 310
1 CPallini 310
2 George Jonsson 269
3 Gihan Liyanage 255
4 OriginalGriff 245
0 OriginalGriff 4,903
1 CPallini 3,960
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,786
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100