0

I've been stuck into a problem for several hours now. I'm building a stored procedure in MYSQL with lots of Inserts in new physical tables. The following is just a sample that I'm unable to get running in my MySQL server.

DROP PROCEDURE IF EXISTS `someProc`$$

CREATE DEFINER=`test`@`%` PROCEDURE `someProc`()
BEGIN


DROP TABLE IF EXISTS table1;

SET @SQL1 = '   CREATE TABLE `table1` (`last_updated` INT); 

                INSERT INTO `table1` (`last_updated`)
                VALUES (1); ';
-- select @SQL1;


PREPARE stmt2 FROM @SQL1;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;

END$$

DELIMITER ;

Upon invoking the stored proc, I get a MySQL error code 1064 saying that a error exists around 'INSERT INTOtable1(last_updated).

But I can run the String contents assigned to the @SQL1 variable as a valid MySQL query and get the table created as well as the value inserted into it too. I've googled for the error code and as suggested in the blogs/documentation pages, I don't think I'm using a reserved keyword here nor I'm inserting any incompatible datatypes. It's plain simple SQL that works outside of the stored procedure setting but fails miserably inside a proc. Can anyone shed some light?

2
  • First thought is to remove the space before the (. MySQL can be finicky about spaces before parentheses. Commented May 19, 2014 at 18:22
  • I just tried that but the error remains the same. Commented May 19, 2014 at 18:25

2 Answers 2

1

prepare prepares one statement. This is clear in the documentation:

The text must represent a single statement, not multiple statements.

You need to split these two operations into two different statements.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Gordon for your help. I wish MySQL threw errors exactly specifying the problem rather than the generic codes. It would have been easier for the noobs like me.
@rstarter . . . I agree 1000%. You noticed my original reaction: "Oh it must be that weird thing with MySQL about spaces after functions (even though insert is not a function)." Totally wrong, but then I remembered about prepared statements.
0

use BI; DROP PROCEDURE VCInplantFactProcedure; DELIMITER // Create Procedure VCInplantFactProcedure() BEGIN
INSERT INTO fact_vcinplant_table (trip_id, dw_loader_id, dw_packer_id) select ts.trip_id,dl.dw_loader_id,dp.dw_packer_id from tbl_ap_trip_stagging ts left join dimension_loader dl on ts.loader_id=dl.loader left join dimension_packer dp on ts.packer_id=dp.packer;

END// DELIMITER ;

select insert working normally through stored procedure not working in my sql kindly help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.