Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-29-2013, 12:33 PM   PM User | #1
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 69
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Role of variables in a stored procedure?

Hello, can someone please help me understand what the role defining variables does when it comes to using them with stored procedures.

For example if I write a stored procedure with a variable with a default value of Tim

Code:
DELIMITER //
CREATE PROCEDURE test()
BEGIN
 DECLARE the_name VARCHAR( 255 ) DEFAULT 'Tim'; 
 SELECT name from table where name = the_name;
END //
What's the difference between that and this where I define no variable

Code:
DELIMITER //
CREATE PROCEDURE test()
BEGIN
  SELECT name from table where name = 'Tim';
END //
I can't quite see the difference and why you declare them? Can you pass in values to overwrite these default values and if no value is passed in it uses the default?

Can anyone help me understand the role of variables in stored procedures? Thank you.

Last edited by Oatley; 03-29-2013 at 11:14 PM..
Oatley is offline   Reply With Quote
Old 03-29-2013, 09:49 PM   PM User | #2
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
In your case it is pointless.

The use od declared variables comes in place where you need then and you can't use them unless they are declared: <-- this is the rule (EDIT: this applays to s.p. local variables)

Code:
  DECLARE the_int_variable int DEFAULT 0; 

  Select max (fileld) into  the_int_variable from table;
  /*you can use that in further selects, stored proc cursor loops, conditions,....*/
one more example(suppose you have to do many slects using same date in condition):

Code:
DECLARE d DATETIME; 

select max (datefield) into d from table;

select * from tab1 where datefield = d;
select * from tab2 where datefield = d;
select * from tab3 where datefield = d;
/*get it ?  You are saving db resources here, not calling for date for condition each time*/
You can assign new value to variable at any time, default is there so you are able to check if it has changed during stored proc run.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search

Last edited by BubikolRamios; 03-29-2013 at 10:01 PM..
BubikolRamios is offline   Reply With Quote
Users who have thanked BubikolRamios for this post:
Oatley (03-29-2013)
Old 03-29-2013, 11:14 PM   PM User | #3
Oatley
New Coder

 
Join Date: Sep 2012
Posts: 69
Thanks: 56
Thanked 0 Times in 0 Posts
Oatley is an unknown quantity at this point
Brilliant explanation. Thank you I fully understand now.
Oatley is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:23 AM.


Advertisement
Log in to turn off these ads.