Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a mysql stored procedure, with arguments, from command line?

How can I call a stored procedure from command line?

I have a procedure:

CREATE DEFINER=`root`@`localhost` PROCEDURE `insertEvent`(IN `dateTimeIN` DATETIME)     NO SQL BEGIN     SET @eventIDOut = NULL;      IF  EXISTS(SELECT * FROM `events` WHERE `eventDate` = dateTimeIN) THEN         SELECT `eID` INTO @eventIDOut FROM `events` WHERE `eventDate` = dateTimeIN LIMIT 1;         ELSE         INSERT INTO `events` (`eventDate`) VALUES(dateTimeIN);         SET @eventIDOut = last_insert_id();     END IF;      SELECT CONCAT(@eventIDOut); END 
  1. I tried this: mysql> CALL insertEvent(2012.01.01 12:12:12);

    Result:

    ERROR 1064 (42000): 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 '.01 12:12:12)' at line 1

  2. And this: mysql> CALL insertEvent

    -> 2012.01.01 12:12:12;

    Result:

    ERROR 1064 (42000): 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 '2012.01.01 12:12:12' at line 2

like image 544
Gábor DANI Avatar asked Apr 22 '13 21:04

Gábor DANI


People also ask

How do you call a stored procedure with parameters in MySQL?

Example: Calling a stored procedure with parametersmysql> CREATE TABLE Emp (Name VARCHAR(255), Salary INT, Location VARCHAR(255)); Assume we have created a stored procedure InsertData which accepts the name, salary and location values and inserts them as a record into the above create (Emp) table.

How do I run a stored procedure from the command line?

You can use the command line tool "sqlcmd Utility" from your batch file to connect to a sql server and execute a SQL Statement / stored procedure. you can use SQLCMD to run store procedure from CMD.

How do you call a procedure in MySQL terminal?

MySQL refers to stored procedure execution as calling, and so the MySQL statement to execute a stored procedure is simply CALL . CALL takes the name of the stored procedure and any parameters that need to be passed to it. Take a look at this example: CALL productpricing(@pricelow, @pricehigh, @priceaverage);


1 Answers

With quotes around the date:

mysql> CALL insertEvent('2012.01.01 12:12:12'); 
like image 129
koriander Avatar answered Sep 19 '22 12:09

koriander