Go Back   DeveloperBarn Forums > Databases > MySQL

Sponsored Links

Discuss "Stored Procedures on MySQL" in the MySQL forum.

MySQL - MySQL is a powerful open source database system most commonly used with PHP applications. Discuss MySQL administration and other MySQL related topics here.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old June 9th, 2008, 08:31 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default Stored Procedures on MySQL

I thought I'd try adding some stored procedures to my MySQL db, to see if it improves performance.

I have a list of clients with a number of options to filter the list. At the moment I build the query as a string in .NET and then execute it on the db.

What I am trying to do is pass the various filter options to the SP and build the query in the SP.

How would you go about doing this in an SP?
And, how do you pass parameters to the query inside the SP? Is it the same as you'd pass them to a query?

I tried this simplified version from an example on the MySQL site, but it just returned "Number of rows affected:-1", which I believe means there's an error somewhere.
Code:
CREATE PROCEDURE clientlist
(pcuser INT, istatus INT)
 
BEGIN
DECLARE _status VARCHAR (50);
IF istatus = 5 THEN
       SET _status = ' AND visitors.status<>5';
ELSE
       SET _status = ' AND visitors.status=1';
END IF
 
SET @strsql = CONCAT('SELECT visitorref,forename,surname,clientstatus.status FROM visitors,clientstatus WHERE visitors.status=clientstatus.statusref',_status);
 
PREPARE stmt FROM @strsql;
 
EXECUTE stmt;
 
END;
Can anyone see what I'm doing wrong or provide an example?

Thanks
Reply With Quote
Sponsored Links
  #2  
Old June 9th, 2008, 10:11 AM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

Does mySQL have anything like the T-SQL PRINT statement? If so, put a PRINT stmt in before the EXECUTE to check that you sql statement is correct. I notice that you don't declare strsql -- T-SQL wouldn't like this.
__________________
Wolffy
------------------------
Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.
Reply With Quote
The Following User Says Thank You to Wolffy For This Useful Post:
richyrich (June 9th, 2008)
  #3  
Old June 9th, 2008, 10:12 AM
mehere's Avatar
Super Sarcasm Mistress


 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 143
Thanks: 10
Thanked 27 Times in 25 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

i don't see anything different in what you're doing, then what i'm finding.
Code:
DELIMITER \\

CREATE PROCEDURE clientlist (
pcuser INT, 
istatus INT
)
BEGIN
	DECLARE _status VARCHAR (50);
	
	IF istatus = 5 THEN
       		SET _status = 'AND visitors.status<>5';
	ELSE
       		SET _status = 'AND visitors.status=1';
	END IF
 
	SET @strSQL = CONCAT('SELECT visitorref,forename,surname,clientstatus.status FROM visitors,clientstatus WHERE visitors.status=clientstatus.statusref ',_status);
	
	PREPARE stmt1 FROM @strSQL;
	EXECUTE stmt1;
	DEALLOCATE PREPARE stmt1;
END \\

DELIMITER ;
to use it, just
Code:
CALL clientlist('pc_user',5)
at least that's all it is according the mySQL manual.

Comments on this post
Wolffy agrees: Yeah, I can't seem to find any example that's different from what he has here.
richyrich agrees: Nope...I can't see anything wrong with it either...
__________________
Quote of the Month:
Regret: It hurts to admit when you make mistakes - but when they're big enough, the pain only lasts a second.

Questions to Ponder:
Could it be that all those trick-or-treaters wearing sheets aren’t going as ghosts but as mattresses?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
Reply With Quote
  #4  
Old June 9th, 2008, 10:21 AM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

I take back what I said about strsql -- I now see that @-sign syntax is a thing called global variables -- so different from T-SQL.
Reply With Quote
  #5  
Old June 9th, 2008, 10:39 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

Yeah, I know. I've tried all sorts of things. It creates the procedure fine, but just won't run it.

I've even tried something really basic:-
Code:
CREATE PROCEDURE p4 ()
SELECT 'Hello, world';
which is straight out of a MySQL document.
And
Code:
CREATE PROCEDURE p4 ()
BEGIN
SELECT 'Hello, world';
END;
It creates the SProc fine but when I call it, I just get Affected rows: -1.

I've posted on the MySQL forum to see if they can suggest anything.

Very frustrating...
Reply With Quote
  #6  
Old June 9th, 2008, 10:43 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

And for some reason it doesn't like DELIMITER

It says I have an error in my MySQL statement.

*sigh*
Reply With Quote
  #7  
Old June 9th, 2008, 11:09 AM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

I think that delimiter is just if you are entering the sproc from the console line by line (the equivalent of iSQL) since the semicolon is required sytanx in the sproc itself.
Reply With Quote
  #8  
Old June 9th, 2008, 11:15 AM
richyrich's Avatar
Moderator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 395
Thanks: 26
Thanked 32 Times in 32 Posts
Blog Entries: 1
Rep Power: 1
richyrich will become famous soon enough

Awards Showcase
Classic ASP JavaScript 
Total Awards: 2

Default

OK. I think I may have sorted it.

I downloaded the most recent MySQL Query browser, typed it into that and it executed with no problem.

Called the SProc and it worked fine.

Must be related to the software I was using. I found a post about a similar problem on the MySQL forum and thought I'd give it a go..

Strange how the software you're using can affect what gets saved but there you go!!

Shame I've wasted a whole day finding that out...
Reply With Quote
Reply

  DeveloperBarn Forums > Databases > MySQL

Bookmarks

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
If statement stored procedure peebman2000 SQL Development 32 May 23rd, 2008 03:54 PM
Dynamic Stored Procedure jmurrayhead Microsoft SQL Server 16 March 26th, 2008 11:19 AM
Permissions on Tables, Stored Procedures, etc. theChris Microsoft SQL Server 2 March 24th, 2008 11:49 AM


All times are GMT -4. The time now is 08:15 AM.



Content Relevant URLs by vBSEO 3.2.0