+ Reply to Thread
Page 2 of 2 FirstFirst 1 2
Results 11 to 15 of 15

Thread: Using Parameterized Stored Procedure

  1. #11
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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...

  2. #12
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    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. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #13
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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.

  4. #14
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    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
    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. Void where prohibited by law. Not valid in California. Your mileage may vary.

  5. #15
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    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 to Thread
Page 2 of 2 FirstFirst 1 2

Similar Threads

  1. Stored Procedures on MySQL
    By richyrich in forum MySQL
    Replies: 7
    Last Post: June 9th, 2008, 12:15 PM
  2. Parameterized Dynamic Query
    By Shem in forum .NET Development
    Replies: 4
    Last Post: June 6th, 2008, 11:20 AM
  3. If statement stored procedure
    By peebman2000 in forum SQL Development
    Replies: 32
    Last Post: May 23rd, 2008, 04:54 PM
  4. Dynamic Stored Procedure
    By jmurrayhead in forum Microsoft SQL Server
    Replies: 16
    Last Post: March 26th, 2008, 12:19 PM

Bookmarks

Posting Permissions

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

SEO by vBSEO