Creating a stored procedure in MySQL5 with ColdFusion 9's <CFQUERY>-Tag

假装没事ソ 提交于 2019-12-13 15:22:49

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/9554775/creating-a-stored-procedure-in-mysql5-with-coldfusion-9s-cfquery-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!