Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (4k points)

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 Answer

0 votes
by (8.7k points)

Stored procedure: A stored procedure is an SQL Query that you can save and run whenever you need the query in the future. It helps to increase the reusability of the code as we don’t have to write the entire query again and save time by just calling that query by a  name.

 

Let’s have a look mon code how to call  it :

 

CREATE DEFINER=`admin`@`localhost` PROCEDURE `invoking`(IN `dateTimeIN` DATETIME)

    NO SQL

BEGIN

    SET @scheduleIDOut = NULL;

 

    IF  EXISTS(SELECT * FROM `schedule` WHERE ‘scheduleDate` = dateTimeIN) THEN

        SELECT `sID` INTO @scheduleIDOut FROM `schedule` WHERE `scheduleDate` = dateTimeIN LIMIT 1;

        ELSE

        INSERT INTO `schedule` (`scheduleDate`) VALUES(dateTimeIN);

        SET @scheduleIDOut = last_insert_id();

    END IF;

 

    SELECT CONCAT(@scheduleIDOut);

END

 

Calling procedure eith argument from  command line:

 

mysql> CALL invoking ('2021.05.02 12:12:12');

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jun 26, 2020 in SQL by Sudhir_1997 (55.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...