Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    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.
    Last edited by BubikolRamios; 03-29-2013 at 10:01 PM.
    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

  • Users who have thanked BubikolRamios for this post:

    Oatley (03-29-2013)

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    76
    Thanks
    61
    Thanked 0 Times in 0 Posts
    Brilliant explanation. Thank you I fully understand now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •