So im creating a procedure that accepts an argument namee and returns all users whose name is a substring of namee
create procedure search_res(IN namee varchar(50))
begin
select * from usr where name like %namee%;
end
But im 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 '%namee%; end' at line 3.
What is the correct syntax?
Use CONCAT function
select * from usr where name like CONCAT('%',namee,'%');
Related
I am attempting to recreate a stored procedure (since I can't edit the body). I called SHOW CREATE PROCEDURE to use the same format as the original stored procedure but when I attempt to recreate it I get the following errors:
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 11
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 'DECLARE organization_id BIGINT(20) UNSIGNED' at line 1
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 'DECLARE lobby_pod_id BIGINT(20) UNSIGNED' at line 1
Here's the code:
CREATE DEFINER=`lms`#`10.0.0.%` PROCEDURE `create_organization`(
IN admin_username VARCHAR(255),
IN organization_name VARCHAR(100)
)
BEGIN
DECLARE admin_user_id BIGINT(20) UNSIGNED;
DECLARE organization_id BIGINT(20) UNSIGNED;
DECLARE lobby_pod_id BIGINT(20) UNSIGNED;
SELECT ID, account INTO admin_user_id, organization_id
FROM users
WHERE username = admin_username;
INSERT INTO pods (`title`, `description`, `owner`, `scene`)
VALUES (CONCAT(organization_name, " Village"),
CONCAT("General meeting space and hub for ", organization_name, " students and teachers."),
admin_user_id,
" Village"
);
END
I pasted into SQL Fiddle and got the same result, although pasting into MySQL Syntax Check gave me the thumbs-up. I'm sure it's a simple miss but it isn't that obvious to me.
You are missing the delimiter definition before and after the stored proc definition:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. [...] The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Since the stored proc definition and body was ok, syntax chack gave you the thumbs up, but the code would not run properly in your client.
Use the following skeleton for defining a stored procedure:
delimiter //
create procedure ...
...
end
//
delimiter ;
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
i trie to run this statement:
CREATE DEFINER=`daimler_bkUser`#`%` PROCEDURE `expatSuchen`(IN SUCHBEGRIFF VARCHAR(60))
BEGIN
Select * from expats where Last_Name LIKE CONCAT('%', SUCHBEGRIFF , '%');
END
But MySQL gives me the following error:
java.sql.SQLSyntaxErrorException: 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
I am getting this 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 'if (t=null) then update employee set grade='x' where empid=id; END' at line 4
I am not able to understand where the syntax is wrong.
I am creating a procedure to get a grade from the table if it grade is present. If it is not present then it should be updated as x.
CREATE PROCEDURE spGETgrade (in id int)
BEGIN
select grade as t from employee where empid=id
if (t=null) then
update employee set grade='x' where empid=id;
END $$
I think you need ";" after the first query
select grade as t from employee where empid=id; <<---
and
IF THEN
...
END IF; <<--
I am attempting to recreate a stored procedure (since I can't edit the body). I called SHOW CREATE PROCEDURE to use the same format as the original stored procedure but when I attempt to recreate it I get the following errors:
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 11
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 'DECLARE organization_id BIGINT(20) UNSIGNED' at line 1
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 'DECLARE lobby_pod_id BIGINT(20) UNSIGNED' at line 1
Here's the code:
CREATE DEFINER=`lms`#`10.0.0.%` PROCEDURE `create_organization`(
IN admin_username VARCHAR(255),
IN organization_name VARCHAR(100)
)
BEGIN
DECLARE admin_user_id BIGINT(20) UNSIGNED;
DECLARE organization_id BIGINT(20) UNSIGNED;
DECLARE lobby_pod_id BIGINT(20) UNSIGNED;
SELECT ID, account INTO admin_user_id, organization_id
FROM users
WHERE username = admin_username;
INSERT INTO pods (`title`, `description`, `owner`, `scene`)
VALUES (CONCAT(organization_name, " Village"),
CONCAT("General meeting space and hub for ", organization_name, " students and teachers."),
admin_user_id,
" Village"
);
END
I pasted into SQL Fiddle and got the same result, although pasting into MySQL Syntax Check gave me the thumbs-up. I'm sure it's a simple miss but it isn't that obvious to me.
You are missing the delimiter definition before and after the stored proc definition:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. [...] The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Since the stored proc definition and body was ok, syntax chack gave you the thumbs up, but the code would not run properly in your client.
Use the following skeleton for defining a stored procedure:
delimiter //
create procedure ...
...
end
//
delimiter ;