Hello, I implemented a stored procedure and can successfully create it from mysql and call it from my php application. Now, I am trying to put the create in my module's install file so the upgrade will take care of sp creation but I keep getting 'sql syntax' error when I run the upgrade.php. Please note that the same sql executes without any errors when I run it from mysqladmin/phpMyadmin.
Any help is appreciated.

Comments

therealwebguy’s picture

Please post the entire SQL error that drupal is displaying so that we can better assist you in resolving your issue.

tmnt09’s picture

This is the error I get:

user warning: 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 $ CREATE PROCEDURE sp_getOrgs() BEGIN DECLARE loop' at line 1 query: update_sql

in C:\workspace\2.x\includes\database.mysqli.inc on line 156

Thank you.

alexander allen’s picture

I had the same problem you had. Eliminate the "DELIMITER $", it is not necesarry when you create the SP from PHP.

For example this is how I do it:

  • First I store the store procedure in a file ending with .sql, since some SPs can be quite large:

    Example filename: sp_mystoredprocedure1.sql

    CREATE  PROCEDURE sp_mystoredprocedure1( )
    BEGIN
     SELECT node.nid AS nid,
       node.title AS node_title,
       node_revisions.teaser AS node_revisions_teaser,
       node_revisions.format AS node_revisions_format,
       node.created AS node_created,
       content_type_story.field_image_slideshow_fid AS field_image_slideshow_fid,
       files.filepath AS files_filepath,
       users.name AS author
     FROM node node
     LEFT JOIN node_revisions node_revisions ON node.vid = node_revisions.vid
     LEFT JOIN content_type_story content_type_story ON node.vid = content_type_story.vid
     LEFT JOIN files files ON content_type_story.field_image_slideshow_fid = files.fid
     LEFT JOIN users users ON node.uid = users.uid
     WHERE (node.status <> 0) AND (node.type in ('story')) AND content_type_story.field_slideshow_slideshow_value = 1
       ORDER BY node.created DESC LIMIT 4;
    END
    
    # end procedure -- this is just a comment, you can delete it
    
  • Then from your module's PHP code you can read the *.sql file and successfully call the SQL query above (in this case a stored procedure). Demonstration:
    # The module's installation hook
    function yourmodulename_enable() {
    	
    	$module_path = drupal_get_path('module', 'yourmodulename');
    	$stored_procedures = array('sp_mystoredprocedure1.sql', 'sp_mystoredprocedure2.sql', 'sp_mystoredprocedure3.sql', ...);
    	
    	foreach ($stored_procedures as $file) {
    		$sp_string = file_get_contents("$module_path/stored_procedures/$file");
    		$result = db_query($sp_string);
    	}
    	
    	drupal_set_message("Database stored procedures created.", 'status');
    }
    
    

Once you run that SQL code from PHP, MySQL automatically adds the "DELIMITER $" when it "stores" the procedure. You can verify this by opening the procedure for editing using a MySQL command or GUI editor like MySQL Query Editor.