Go Back   DeveloperBarn Forums > Databases > MySQL

Sponsored Links

Discuss "Using Parameterized Stored Procedure" 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
  #11  
Old June 11th, 2008, 09:29 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

The reason I was looking at the SP was this particular query always seems to take quite a long time to run in .NET, whereas when I tested it in the query browser it was taking less than half a second.

I'm sure the delay is DB related as other queries to the same table are much quicker...
Reply With Quote
Sponsored Links
  #12  
Old June 11th, 2008, 10:10 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

RR;
I didn't take the time to unravel all the logic in your SP, but it appears in mySQL that you can PREPARE..EXECUTE a query whilst substituting parameters. In you case, consider if you can build your dynamic query in such a way to use parameters in the EXECUTE statement rather than building them into the query. Such as the very simple example:
Code:
     SET @s = 'SELECT * FROM authors WHERE id BETWEEN ? AND ?'; 
    SET @min = _min; 
    SET @max = _max; 

    PREPARE stmt1 FROM @s; 
    EXECUTE stmt1 USING @min, @max; 
    DEALLOCATE PREPARE stmt1;
__________________
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 11th, 2008)
  #13  
Old June 11th, 2008, 02:28 PM
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 did see that Wolffy, but wasn't sure if I could get the order of the parameters right. I assume it drops the USING values into the query in the order they are placed.

I was wondering if you could do the same but name them.
Reply With Quote
  #14  
Old June 11th, 2008, 02:58 PM
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

Yeah, now I see what you mean. I was trying to figure out a way to do this while having the same parameters in the same position regardless of query -- but that's going to make for some really inefficient queries. Take the rather simple example of your conveyor predicate:
Code:
IF iconveyor <> '' AND iconveyor<>'all' THEN
 SET _conveyor = ' AND visitors.conveyor = ?';
ELSE
 SET _conveyor = ' AND visitors.conveyor LIKE ?;
 SET @iconveyor = '%';
END IF;
Consider all the permutations, and you could have a ton of LIKE's in the query. Gross
Reply With Quote
  #15  
Old June 12th, 2008, 07:00 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

I think I got it sorted.

No changes to my SP. In .NET used the following code to add the parameters:-
Code:
Dim mycommand as new mysqlcommand("clientlist",conn)
Dim isurname as MySqlParameter = mycommand.parameters.Add("isurname",mysqldbtype.varchar)
isurname.value = search.surname
So, you need to create an instance of a mysql parameter object and set it to an IN parameter of the SP. You need to create an object for all the IN parameters you have in your SP. Then assign the value you want to it. In my case it's a property of a search class I have.

Hope that helps someone...
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
Stored Procedures on MySQL richyrich MySQL 7 June 9th, 2008 11:15 AM
Parameterized Dynamic Query Shem .Net Development 4 June 6th, 2008 10:20 AM
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


All times are GMT -4. The time now is 11:25 AM.



Content Relevant URLs by vBSEO 3.2.0