Question

I wonder if it's possible to create a stored procedure in MySQL5 via ColdFusion's <cfquery>-tag. I've never done anything with storedprocedures before...

I was trying to set a function which replaces like MySQL's REPLACE but case insensitive. I wanted to use the function provided here.

But first I want to create this function via Coldfusion like:

<CFQUERY datasource="#dsn#">
    DELIMITER $$

    DROP FUNCTION IF EXISTS `replace_ci`$$
    CREATE FUNCTION `replace_ci` ( str TEXT,needle CHAR(255),str_rep CHAR(255))
    RETURNS TEXT
    DETERMINISTIC
    BEGIN
    DECLARE return_str TEXT;
    SELECT replace(lower(str),lower(needle),str_rep) INTO return_str;
    RETURN return_str;
    END$$

    DELIMITER ;
</CFQUERY>

This throws following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ DROP FUNCTION IF EXISTS replace_ci$$ CREATE FUNCTION' at line 1

Although executing the statement directly in e.g. phpMyAdmin succeeded.

This article shows only how to call a storedprod with <cfstoredproc>-tag but I cannot see where these procedures are declared to the databaseserver.

Was it helpful?

Solution

maybe you're making this more complicated as it should be. I found the following discussion stating, you dont'd need DELIMIER keyword: http://forums.mysql.com/read.php?39,130834,248556#msg-248556

<CFQUERY datasource="mysql_jdbc">
    DROP FUNCTION IF EXISTS `replace_ci`;

    CREATE FUNCTION `replace_ci` ( str TEXT,needle CHAR(255),str_rep CHAR(255))
        RETURNS TEXT
         DETERMINISTIC
        BEGIN
        DECLARE return_str TEXT;
        SELECT replace(lower(str),lower(needle),str_rep) INTO return_str;
        RETURN return_str;
     END
</CFQUERY>

Don't forget to add allowMultiQueries=true to your JDBC URL: http://www.bennadel.com/blog/1542-MySQL-3-4-com-mysql-jdbc-Driver-And-allowMultiQueries-true.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top