How to create stored procedures in SQL using cmd? - mysql

This is what i tried. I have table called crop, and want to create stored procedure of selecting all in that table.
Here is the error i got:
MariaDB [sample]> CREATE PROCEDURE AllFarmers
-> AS
-> SELECT * FROM crop
-> GO;
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MariaDB server version for the right
syntax to use near 'AS SELECT * FROM crop GO' at line 2
Thank you for your help in advance

I saw that the syntax is not correct on your procedure. You should replace AS by BEGIN and GO by END.
I put an example here
delimiter //
CREATE PROCEDURE AllFarmers ()
BEGIN
SELECT * FROM Test;
END;
//
call allfarmers()
Try it and tell me if there is any problem

Might also be worth mentioning you can a drop to the start in case the stored procedure already exists:
DROP PROCEDURE IF EXISTS `AllFarmers`

Related

#1064 Error SQL - Happens on all my procedures/functions

I know this question has been asked a bunch in different forms, but none of the ones I looked at (quite a bit) seemed to help me out in my specific case. I wrote a few functions and procedures and I always get the same error at the same spot. Here is my code:
DELIMITER |
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId, Name FROM STUDENT WHERE Status = sts;
END |
DELIMITER;
This happens on all my procedures functions, this 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 'CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId,' at line 2
On my other one its this:
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 'CREATE FUNCTION GoodGrade(letGrade VARCHAR(2)) RETURNS int
BEGIN
DECLARE v' at line 2
This happens whether or not I use | or // as the DELIMITER...can someone tell me what I'm doing wrong here? Thanks!
You've changed the delimiter, written a statement and then not delimited it according to your new definition
Change the line
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
to
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS |
Update
This wasn't obvious from the initial error message, but you have another error when you reset the Delimiter after the procedure definition. You need a space before the ';' on that one.
so change
DELIMITER;
to
DELIMITER ;
NB
The other use of ';' within your procedure definition is correct, because you want everything within the create statement to to be processed together.

Finding errors in Mysql Stored Procedure. Unable to understand the error

What can be the issue in this stored procedure? I have syntax error in this code which I am unable to understand. The sql query inside the code runs perfectly fine but when I try to write it as a stored procedure it throws syntax error.
The error is:
[ERROR in query 1] PROCEDURE 1bot already exists
[ERROR in query 2] 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' at line 1
DELIMITER ;;
CREATE PROCEDURE `1bot`()
BEGIN
SELECT
COUNT(distinct orders.customer_id) as Total_Customers,
date_format(orders.created_at,'%M %Y') as month,
COUNT(orders.`increment_id`) as Unique_single_orders,
SUM(orders.`base_subtotal`) as total_rev,
SUM(billing.`total` + 5) as COGS, (
SUM(orders.`base_subtotal`) - SUM(billing.total + 5)) as marketing_expense,
(AVG(orders.`base_subtotal`) - AVG(billing.total + 5)) as avg_acquisition_cost
FROM `sales_flat_order` as orders
LEFT JOIN `sales_flat_order_item` as items on orders.`entity_id` = items.`order_id`
LEFT JOIN `vingo_billing` as billing on orders.`increment_id` = billing.`order_number`
WHERE orders.`created_at` between '2016-09-01 00:00:00' and '2017-01-23'
AND items.sku LIKE "%1bot%"
AND orders.status != "closed"
and orders.shipping_invoiced != '15'
and billing.total > 0
and orders.`total_qty_ordered` < 2;
END ;;
DELIMITER ;
PROCEDURE 1bot already exists
This is self-explanatory. You apparently already have created a procedure named 1bot. MySQL gives you an error because the alternative would be to silently replace the existing procedure with the one you are defining, which may be different. MySQL is protecting you from clobbering a procedure you might need.
If you want to clobber the existing procedure and replace it with new code, you must drop it first:
DROP PROCEDURE `1bot`;;
CREATE PROCEDURE `1bot`() ...
Note that a similar error is given if you try to CREATE TABLE and name a table that already exists.
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'
You should know that the DELIMITER statement is only recognized in the mysql command-line client. The purpose is to change the statement delimiter, which is normally ; or \G, because otherwise you couldn't define a stored procedure or trigger with a body that contains multiple semicolon-delimited statements.
Whereas in most other interfaces, e.g. phpMyAdmin, MySQL Workbench, or when running SQL statements in an application, you can only run one statement at a time anyway, so there's no chance of ambiguity. Therefore DELIMITER is not necessary—and not allowed—in those interfaces.

Can't create MySQL procedure to update table

I am trying to make a procedure to update an existing user. It receives the name of the user and then increments his points column. I have it like this:
CREATE PROCEDURE addPoints (IN nomeus varchar(20))
BEGIN
UPDATE User
SET
points=points+1
WHERE (nome=nomeus) ;
END;
However, I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 8
How can I fix it?
When in doubt just go to dev.mysql they have some great documentation there.
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
Looks like you found the answer to your own question. However, if you change any of the code in the procedure, don't forget to drop the procedure before trying to create it again.
Drop Procedure if exists addPoints;
http://dev.mysql.com/doc/refman/5.0/en/drop-procedure.html
Yeah, I had to use delimiters:
delimiter //
CREATE PROCEDURE addPoints(IN nomeuser varchar(256))
BEGIN
UPDATE User
SET
points = points + 1
WHERE nome = nomeuser;
END;//
delimiter ;

Creating Stored procedures in MySql/phpmyadmin

I have already created my database with tables and now I want to create stored procedures for my tables but I keep getting an error.
This is how I try creating the stored procedure in SQL:
DELIMITER //
CREATE PROCEDURE sp_accesslevel_Select()
BEGIN
SELECT * FROM accesslevel;
END //
DELIMITER;
And the error I get is:
"#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 //
CREATE PROCEDURE sp_accesslevel_Select()
BEGIN
SELECT * FROM ' at line 1 "
Can anyone help me fix this problem?
try to go in "Routines" menu and create it from there

MySQL stored proc not getting created

So I have this stored proc that will not get created when I run the file.
DELIMITER //
DROP PROCEDURE IF EXISTS msd.test_proc//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM
msd.zipcode_lookup;
END//
DELIMITER ;
When I run this I get an error code 1064 at line 1 when I execute in RazorSQL. Here is the complete error message:
ERROR: 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 '//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM ' at line 1
Error Code:1064
I've tried other variations and still get errors. I am sure this is something basic I am missing. I appreciate any help.
Thanks.
As stated on the RazorSQL website:
The DELIMITER statement is not part of the MySQL language. It is a command supported by certain MySQL tools. This command tells those MySQL programs to scan for a certain character that indicates the end of a query or statement.
RazorSQL does not support using the DELIMITER command. The SQL statement delimiter value used by RazorSQL can be changed using the preferences window. The default values is the semi-colon.