If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > call stored procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-29-11, 06:46
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
call stored procedure

call stored procedure
I've written a stored procedure as follows: I need to be able to enter a judge_id to retrieve judge_name and judge_suburb. The fields in my table are judge_id, judge_name and judge_suburb. The procedure appears to be error free.
I now need to write a statement to call the procedure so I can test it. I'm having difficulty finding guidance for this syntax for MySQL. Can anyone point me in the right direction, please?


delimiter //

drop procedure if exists judge_sp //
create procedure judge_sp
(in judge_id int,
out judge_name varchar(20),
judge_suburb varchar(30))
begin
select judge_name into judge_name
from judge
where id = judge_id;
select judge_suburb into judge_suburb
from judge
where id = judge_id;

end;
//

delimiter ;
Reply With Quote
  #2 (permalink)  
Old 03-29-11, 08:20
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
To call a stored procedure in MySQL you have to use "CALL judge_sp(input, @x, input);"

I would group all input fields together. Rather than having input, output and then another input field as parameters to the stored procedure. The @x is a session variable. To view the value of that issue "SELECT @x;"

I have just looked at your stored procedure again and there are improvements that could be made:

Code:
CREATE PROCEDURE judge_sp (IN judge_id      INT,
                           OUT judge_name   VARCHAR(20),
                           OUT judge_suburb VARCHAR(30))
BEGIN
  SELECT judge_name,
         judge_suburb
  INTO   judge_name, judge_suburb
  FROM   judge
  WHERE  id = judge_id;
END;
Also consider what should happen if someone requests a judge_id that does not exist in the database table? There are handlers that you can use for this. A handler is defined as follows:

Code:
CREATE PROCEDURE judge_sp (IN judge_id      INT,
                           OUT judge_name   VARCHAR(20),
                           OUT judge_suburb VARCHAR(30))
BEGIN
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET judge_name = 'Not found';
  SELECT judge_name,
         judge_suburb
  INTO   judge_name, judge_suburb
  FROM   judge
  WHERE  id = judge_id;
END;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com

Last edited by it-iss.com; 03-29-11 at 08:25.
Reply With Quote
  #3 (permalink)  
Old 03-29-11, 17:26
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
call stored procedure

Thanks very much, Ronan.
Reply With Quote
  #4 (permalink)  
Old 03-30-11, 00:02
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
call stored procedure - parameters

With the call statement, Ronan, I don't understand the three parameters this requires.
I ran the select @x statement and it returned a NULL.
Sorry to be thick! I thought the only input that would be required would be the judge id.
Reply With Quote
  #5 (permalink)  
Old 03-30-11, 01:17
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
I've been reading up on sessions and found it helpful but it didn't give me enough to apply to the situation I'm working with.
Reply With Quote
  #6 (permalink)  
Old 03-30-11, 03:06
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
For your information @name are MySQL user variables. You can create as many as you want. Stored procedures can have none, one or more than one parameter passed in or out of the stored procedure. The type of parameter IN or OUT is defined before the parameter name and data type in the CREATE PROCEDURE. A parameter without a type by default is an IN parameter.

In the case above, there is one IN parameter and two out parameters. To make this work what you would need to do is:

Code:
CALL judge_sp(1, @name, @suburb);
The 1 is some judge identifier value.

The user variable @name will contain the judge name and @suburb the suburb. To view these in MySQL simply execute:

Code:
SELECT @name, @suburb;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #7 (permalink)  
Old 03-30-11, 05:18
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
Thanks for your excellent explanations, Ronan. I believe I'm learning heaps and I understand the CALL statement and the parameters now.
However, I'm getting an error message when I run the CALL statement:

ERROR 1054 (42S22): Unknown column 'id' in 'where clause'
When I previously ran the CREATE stored procedure, the Query was OK (I'm using the one with the handler added in).
I tried changing the where statement to

WHERE judge_id = judge_id;

but it still returned the same error message.
Reply With Quote
  #8 (permalink)  
Old 03-30-11, 10:56
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi,

actually it is not wise to have the parameters named the same as the individual fields that they access. This can lead to odd behaviours. I suggest renaming the parameters to include a leading p to indicate that these are actually parameters:

Code:
CREATE PROCEDURE pjudge_sp (IN judge_id      INT,
                           OUT pjudge_name   VARCHAR(20),
                           OUT pjudge_suburb VARCHAR(30))
BEGIN
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET pjudge_name = 'Not found';
  SELECT judge_name,
         judge_suburb
  INTO   pjudge_name, pjudge_suburb
  FROM   judge
  WHERE  id = pjudge_id;
END;
Check your original code and there you specified id when searching the judge table. Drop the original stored procedure and reload this stored procedure.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #9 (permalink)  
Old 04-01-11, 04:56
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
Thanks, Ronan: that's very clear direction with the parameters.
I edited my code to reflect the parameter changes and edited the call statement to:

call pjudge_sp(4, @name, @suburb);

This still evoked the same where clause error message.

So I tried changing my 'where' clause to where judge_id = pjudge_id.
This changed the error message to "unknown column 'pjudge_id' in 'where clause'.
I can't see what else to try in this clause: my call statement is correct, isn't it?
Reply With Quote
  #10 (permalink)  
Old 04-01-11, 06:41
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Can you give the table definition of judge?
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #11 (permalink)  
Old 04-01-11, 07:44
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
judge_id int not null auto_increment primary key,
judge_name varchar(20) not null,
judge_address varchar(30) not null
Reply With Quote
  #12 (permalink)  
Old 04-01-11, 10:12
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi, I think I made a typo in the previous posting with regards the stored procedure. It should have been:

Code:
CREATE PROCEDURE judge_sp (IN pjudge_id      INT,
                           OUT pjudge_name   VARCHAR(20),
                           OUT pjudge_suburb VARCHAR(30))
BEGIN
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET pjudge_name = 'Not found';
  SELECT judge_name,
         judge_suburb
  INTO   pjudge_name, pjudge_suburb
  FROM   judge
  WHERE  judge_id = pjudge_id;
END;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #13 (permalink)  
Old 04-01-11, 22:29
melgra70 melgra70 is offline
Registered User
 
Join Date: Mar 2011
Location: Sydney, Australia
Posts: 58
Thanks, Ronan. That's so cool! Thanks for being an excellent tutor.
I have a remaining thread still being looked at by dav1mo but I'm waiting on his further guidance (calculate query and subquery).
The other one is the triggers question which I'll investigate further.
Thanks again for your help.
Mel.
Reply With Quote
  #14 (permalink)  
Old 04-06-11, 11:11
niranjana niranjana is offline
Registered User
 
Join Date: Apr 2011
Posts: 3
tool to run store procedure

Hi,

Is there any open source tool to run the store procedure

thanks
Reply With Quote
  #15 (permalink)  
Old 04-06-11, 11:35
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
mysql command line utility allows stored procedures to be called, this can be called via PHP, I am pretty sure also phpMyAdmin will allow stored procedures to be called as well as MySQL Workbench.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On