MySQL Forums
Forum List  »  Stored Procedures

Dynamic Sql Statement in SP
Posted by: Ming Yeung
Date: January 09, 2006 06:22AM

Hello,

I have been working on this for ages, but still no luck. I believe many newbies have the same problem with the MySQL Stored Procedure... I have the SP as below:

CREATE PROCEDURE `Search`(IN inUsername VARCHAR(20), IN inRole CHAR, IN inStatus CHAR, etc...)
BEGIN

SET @varSqlPrefix = "SELECT * FROM admin";
SET @varSql = ' ';

IF inUsername IS NOT NULL THEN

IF @varSql != ' ' THEN
SET @varSql = CONCAT(@varSql, " AND");
END IF;

SET @varSql = CONCAT(@varSql, " username='", inUsername, "'");

END IF;

IF inRole IS NOT NULL THEN

IF @varSql != ' ' THEN
SET @varSql = CONCAT(@varSql, " AND");
END IF;

SET @varSql = CONCAT(@varSql, " role='", inRole, "'");

END IF;

...etc ... so on...

IF @varSql IS NOT NULL THEN
SET @varSql = CONCAT(' WHERE', @varSql);
END IF;

SET @fullSQL = CONCAT(@varSqlPrefix, @varSql);

PREPARE STMT FROM @fullSQL;
EXECUTE STMT;

END

This SP gets the pass-in variables and generate a dynamic Sql statement depends on the pass-in variables, as you can see. I have been searching around on google, but with no luck as SP in MySQL is still new.

What do I actually do?

Thanks guys.
Ming

Options: ReplyQuote


Subject
Views
Written By
Posted
Dynamic Sql Statement in SP
2917
January 09, 2006 06:22AM
1579
January 09, 2006 08:06AM
1308
January 09, 2006 02:10PM
1887
January 09, 2006 08:02PM
1763
January 10, 2006 12:24AM
1579
January 10, 2006 03:07AM
1782
January 10, 2006 06:51AM
1489
January 10, 2006 01:49PM
1837
January 10, 2006 06:35PM
1636
January 10, 2006 07:22PM
1722
January 10, 2006 10:33PM
1734
January 11, 2006 03:10AM
2287
January 11, 2006 04:27AM
1724
January 11, 2006 06:54AM
1574
January 11, 2006 03:34PM
1751
January 11, 2006 03:56PM
1745
January 12, 2006 03:20AM
1531
January 12, 2006 04:44AM
1515
January 11, 2006 09:08PM
1751
January 12, 2006 03:11AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.