![]() |
| |||||||
| Sponsored Links |
![]() | « Previous Thread | Next Thread » |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| ||||
| ||||
| I'm just learning to use Stored Procedures with MySQL. What I'm trying to do is write an SP that returns a client list based on filters passed to it. So I have to build the Query to execute based on the values passed to it.At the moment I have the following code:- PHP Code: Also, how can I alter this SP so that it adds in the value to the query like it would adding a parameter in .NET? Eg. Code: mycommand.Parameters.AddWithValue("surname",mysurnamevar)
Code: mycommand.Parameters.Add("?surname",mysurnamevar)
Code: SET _surname = ' AND surname LIKE @isurname'; Code: SET _surname = ' AND surname LIKE ?surname'; Thanks |
| Sponsored Links |
|
#2
| ||||
| ||||
| Well, since I haven't used MySQL for development in so long, I probably won't be of much help. But in MS SQL, we have a Replace function that could be used to double up the single quotes in the query. See if you can find a similar function for MySQL. As far as the parameters...that would seem logical to do it that way, but I can't be sure.
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.Join our Folding team: DeveloperBarn Folding |
|
#3
| ||||
| ||||
| Yeah, MySQL also has replace, but I think it's only that I'm testing the SP by calling it in query browser with O'xxx as the parameter. I don't think it's a problem with the SP itself, because it'll be fine if I pass the value as a parameter. How would you do that with MS SQL and .NET? |
|
#4
| ||||
| ||||
| Since you're building the query inside the stored procedure by concatenating strings, I wouldn't think it would work. I thought so, too, using MS SQL, however it generated errors, so I had to double up the single quotes. To add parameters using MS SQL, I use: Code: cmd.parameters.AddWithValue("@ParameterName", parameterValue)
|
| The Following User Says Thank You to jmurrayhead For This Useful Post: | ||
richyrich (June 11th, 2008) | ||
|
#5
| ||||
| ||||
| Quote:
|
|
#6
| ||||
| ||||
| When building strings, think of @parameterName as a variable in .Net. You have to concatenate it with the string, not place it between the quotes. So: Code: SET @strSQL= @strSQL + ' AND surname LIKE ''' + REPLACE(@isurname,'''','''''') + ''' ' |
|
#8
| ||||
| ||||
| Quote:
But since you're building them as a string inside the stored procedure, the same rules apply as if you were building a SQL string in code in VB.Net, VBScript, C#, or whatever... |
|
#9
| ||||
| ||||
| Hmmm.....Is there much advantage to using a Stored Procedure in this case then? What about if I built the SQL in .NET and then passed that to a stored procedure and then added the parameters? |
|
#10
| ||||
| ||||
| Honestly, I don't know. I prefer to keep my database queries separate from my code. If you're going to build the query in code, then I don't see a need to pass it to a SP to execute, though. Just execute it from code in that case. |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
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 |