MySQL | MariaDB Creates a #1064 syntax error on import [duplicate] - mysql

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am getting the 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 '' at line 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context

Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html

In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo

Related

Setting a parameter in stored procedure raises an error

I'm trying to create a stored_procedure using the following sql code:
INSERT INTO users (name,surname,email,phone,address_id)
VALUES (name,surname,email,phone,address_id);
SET #GeneratedUserID = LAST_INSERT_ID();
INSERT INTO user_login (username,password_salt,hash_code,users_user_id)
VALUES (username,password_salt,hash_code,#GeneratedUserID );
INSERT INTO user_roles (user_id,role_id)
VALUES (#GeneratedUserID ,1);
However, I'm getting an error:
MySQL said: #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 'SET #GeneratedUserID = LAST_INSERT_ID(); INSERT INTO user_login (username,pas' at line 4
You most likely haven't changed the delimiter, but can't tell for sure, because you didn't post the whole code (please always do this). Therefore MySQL thinks, that the procedure is finished after the first ;.
Your procedure should look like this:
delimiter $$
create procedure x(in p_param1 int, in p_param2 int)
begin
statement1;
statement2;
end$$
delimiter ;
Also note, that you should choose parameter names that are not the same as column names. Best practice is to use a prefix like p_. Same for variables. Use a prefix like v_ or something.

Create a simple stored procedure IN mysql

I'm trying create a simple stored procedure in mysql, In this stored procedure I'm trying to call a view and page it.
delimiter //
CREATE PROCEDURE SelectSearchResultsContract (start int, quantity int)
BEGIN
select
searchresultsdisplayview.CompanyName,
searchresultsdisplayview.LastChanceDate,
searchresultsdisplayview.PhoneNumber,
searchresultsdisplayview.ContactName,
searchresultsdisplayview.City,
searchresultsdisplayview.State
FROM searchresultsdisplayview -- this is a view
OFFSET start
LIMIT quantity ;
END
//
delimiter ;
I cannot create this because of the syntax. says I'm missing a simicolon.
I have create many that we like this using tables but the view doesnt like it. Can someone please tell me what I am missing.
EXACT ERROR:
Error Code: 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 'start LIMIT quantity' at line 15
I Figured this out by right clicking and adding stored procedure then copy and paste the sql that it displayed. below is anwser:
DELIMITER $$
USE `construction_bid_source`$$
CREATE PROCEDURE `SelectSearchResultsContract` (quantity int, start int)
BEGIN
select
searchresultsdisplayview.CompanyName,
searchresultsdisplayview.LastChanceDate,
searchresultsdisplayview.PhoneNumber,
searchresultsdisplayview.ContactName,
searchresultsdisplayview.City,
searchresultsdisplayview.State
FROM searchresultsdisplayview
LIMIT quantity-- this is a view
OFFSET start;
END$$
DELIMITER ;

Error when creating stored procedure to view data in MYSQL

I am having a lot of trouble with the syntax of this stored procedure that is supposed to return All information from a table called Country and is trying to use a parameter for comparison:
Create Procedure CountrybyPK (country char(50))
begin
Select * From Country
Where Country.Name=country
end;
The error I get states:
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 'end' at line 5
You need a statement delimiter between the SELECT statement and END.
Create Procedure CountrybyPK (country char(50))
begin
Select * From Country
Where Country.Name=country;
end;
But since the delimiter would also terminate the whole procedure, you need to use the DELIMITER command to switch to another delimiter:
DELIMITER $$
Create Procedure CountrybyPK (country char(50))
begin
Select * From Country
Where Country.Name=country;
end$$
But since your procedure only has one statement in it, you don't need begin and end at all, you can just do:
Create Procedure CountrybyPK (country char(50))
Select * From Country
Where Country.Name=country;

Pretty printing not working for stored proc in MySQL

I am trying to write an simple stored procedure to just print out information from three different tables. The problem is that for some reason \G does not work from within a stored proc. I want the output to be readable so this is pretty important
This code works but doesnt display columns in an effective way
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername;
END //
DELIMITER ;
This code throws an exception
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername \G;
END //
DELIMITER ;
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 3
Any help would be greatly appreciated
\G is an command in MySQL Client and subsequently can not be called from a sql stored proc

MySQL server stored procedure syntax error

I am having some trouble with creating a Stored Procedure for a MySQL database.
Here is a Select statement that works:
use canningi_db_person_cdtest;
SELECT *
FROM pet
WHERE name = 'Puffball';
Here is my Stored Procedure that does not work:
use canningi_db_person_cdtest;
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END
I am getting the 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 '' at line 5
How can I get this working?
EDIT
How do I call this Stored Procedure? I have tried this with no result:
use canningi_db_person_cdtest;
CALL GetAllPets();
This is the error:
#1312 - PROCEDURE canningi_db_person_cdtest.GetAllPets can't return a result set in the given context
Add a delimiter to end your procedure. Example:
use canningi_db_person_cdtest;
DELIMITER //
CREATE PROCEDURE GetAllPets()
BEGIN
SELECT *
FROM pet
WHERE name = 'Puffball';
END//
If you need more information, please refer to http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html
In this particular case, since you have only one statement in your procedure, you don't need to change DELIMITER nor use BEGIN...END block.
You procedure might look like this
CREATE PROCEDURE GetAllPets()
SELECT *
FROM pet
WHERE name = 'Puffball';
And use it like this
CALL GetAllPets();
Here is SQLFiddle demo