How to create a stored proceedure in mysql? - mysql

Just trying to make a procedure that copies one table into a new one but keep getting all sorts of errors..
eg:
SQL 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 'DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS ' at line 2
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data
END$$
DELIMITER ;`
thanks

You have to end all commands with a semicolon or else mysql will search for more code
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;
END$$
DELIMITER ;
But if you don't have more commands
Make a simple without DELIMITER
CREATE PROCEDURE spClean_T()
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;

Related

MYSQL Creating Procedure

I am trying to create a procedure in MYSQL which just selects all the rows from a table.
DELIMITER $$
CREATE PROCEDURE getAll_Temps()
BEGIN
SELECT * from temp1
END $$
But I am getting this error,
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 '' at line 1.
Try below query.You miss the ; at the end of the query.
DELIMITER $$
CREATE PROCEDURE getAll_Temps()
BEGIN
SELECT * from temp1;
END $$
Hope this helpful to you.

#1064 - check the manual that corresponds to your MariaDB server version for the right syntax?

hi i new in Stored Procedures and want test a procedure for get all the event from my table.
Here is my procedure:
here is the error:
You need to add ;
DELIMITER //
CREATE PROCEDURE GetAllEvents()
BEGIN
SELECT * FROM VERANSTALUNG; -- here
END//
DELIMITER ;
SqlFiddleDemo

Unable to execute Trigger in mysql

I have tables like CallLogs and Activities.Here,My requirement is for each insertion in CallLogs i need an entry in Activities.for that i am writing a trigger and procedure
I am calling procedure from my trigger
Procedure
DELIMITER $$
CREATE PROCEDURE `activity_insert` (IN text TEXT,IN type varchar(250),IN subtype varchar(250),IN date timestamp, IN url TEXT,IN target_id INT(16))
BEGIN
Insert into Activities(text,type,subtype,timestamp,url,Target_ID)values(in_text,in_type,in_subtype,in_date,in_url,in_target_id);
END $$
DELIMITER ;
It is executed successfully
Trigger
DROP TRIGGER IF EXISTS `call_logs_insert`
DELIMITER $$
CREATE TRIGGER `call_logs_insert` AFTER INSERT ON `CallLogs`
FOR EACH ROW
BEGIN
CALL activity_insert('message','Calls',NEW.'type',NEW.'date','null',NEW.'Target_ID');
END;
$$
for some of the columns i am giving static data. and for some of columns i am using new table data
When i am trying to execute this trigger i am getting a following error
#1064 - 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 ''type',NEW.'date','null',NEW.'Target_ID'); END; $$' at line 4
Can any one plz tell me to execute this trigger what are the changes i need to do
Remove single quote from table columns
DROP TRIGGER IF EXISTS `call_logs_insert`
DELIMITER $$
CREATE TRIGGER `call_logs_insert` AFTER INSERT ON `CallLogs`
FOR EACH ROW
BEGIN
CALL activity_insert('message','Calls',NEW.type,NEW.date,'null',NEW.Target_ID);
END;
$$

how to insert rows via loop in MySQL shell (no PHP)

I would like to execute a loop in phpmyadmin which inserts rows in a table. So far I have:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows()
With this, I get an error:
#1064 - 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 PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE ins' at line 1
Syntax for the DROP PROCEDURE statement is incorrect!
Change
DROP PROCEDURE IF EXISTS insert_my_rows()
to
DROP PROCEDURE IF EXISTS insert_my_rows;
You need to end the statement with the proper delimiter. Change END to END$$.
You must change the delimiter only while you make blocks of statements, so during the procedure definition. The DROP PROCEDURE and CALL statements needs delimiters too.
DROP PROCEDURE IF EXISTS insert_my_rows;
DELIMITER $$
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows();
DROP PROCEDURE IF EXISTS insert_my_rows;

mySQL stored procedure error

I am trying to use a if statement in my stored mySQL procedure, but when I try to create it in mySQL workbench I get this error ERROR 1064: 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 'database'.'table' WHERE date=dateIn;.
Here is the code:
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDate`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN EDIT `database`.`table` WHERE date=dateIn;
ELSE SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END$$
I am new to stored procedures, so it's probably a very noob mistake.
Thanx in advance!
date is a reserved word in mySQL. You will have to wrap that in backticks as well.
Here is correct version of your procedure
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDatee`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN
-- I used select below as i don't know what you want in edit either alter table or update table
SELECT * FROM `database`.`table` WHERE date=dateIn;
ELSE
SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END $$