![]() |
| |||||||
| Sponsored Links |
![]() | « Previous Thread | Next Thread » |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| ||||
| ||||
| 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;
Thanks |
| Sponsored Links |
|
#2
| ||||
| ||||
| 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. |
| The Following User Says Thank You to Wolffy For This Useful Post: | ||
richyrich (June 9th, 2008) | ||
|
#3
| ||||
| ||||
| 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 ;
Code: CALL clientlist('pc_user',5)
__________________ 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 |
|
#4
| ||||
| ||||
| 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. |
|
#5
| ||||
| ||||
| 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'; And Code: CREATE PROCEDURE p4 () BEGIN SELECT 'Hello, world'; END; I've posted on the MySQL forum to see if they can suggest anything. Very frustrating... |
|
#7
| ||||
| ||||
| 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. |
|
#8
| ||||
| ||||
| 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... |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
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 |