I need to generate a string like this in stored procedure.
select case when Sex like '%' then 'Person' end as Sex from tableName;
In stored Procedure I have generated like this.
select case when Sex like quote(%) then quote(Person) end as Sex from tableName;
The error i got is
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 '%) then quote(Person) end as Sex from tableName' at line 1 0.000 sec
My MariaDB version is '10.3.16-MariaDB'
Please help in resolving this issue.
You didn't quote the literals in the second query. Try:
SELECT CASE
WHEN sex LIKE quote('%') THEN
quote('Person')
END AS sex
FROM tablename;
But it doesn't make too much sense to quote() literals as you already have to escape any special characters in them. So applying quote() is superfluous.
Related
update amazon-crawler set `flag_images`= '0' where `id`='966'
i get #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 '-crawler set flag_images= '0' where id='966'' at line 1
why syntax error?
amazon-crawler is the table
flag images and id are columns
My guess is that the hyphen in the table name is causing problems, because it is an arithmetic operator. Try also escaping the table name:
UPDATE `amazon-crawler` SET `flag_images`= '0' WHERE `id` = '966';
Note that you should try to avoid using backticks in your query, unless absolutely needed. Using backticks means that any name would potentially work, even one which happens to be a MySQL reserved keyword. Also, I'm guessing that flag_images and id are numeric columns, in which case you should be comparing them against numbers, not strings. So I would write your update as this:
UPDATE `amazon-crawler` SET flag_images= 0 WHERE id = 966;
Here, only the table name has to appear in backticks.
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; <<--
SQL query i'm running in phpMyAdmin :
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES ([1],[Example Company],[image.jpg])
phpMyAdmin generates this, i'm just changing the values.
SQL database:
1 companyId int(100)
2 companyName varchar(100)
3 companyImage varchar(100)
Error I get:
#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 '[1],[Example Company],[image.jpg])' at line 1
Use quotes, not brackets, around strings. Integers and floats don't require anything but MySQL allows quotes around those as well.
INSERT INTO `companies`(`companyId`, `companyName`, `companyImage`)
VALUES (1, 'Example Company', 'image.jpg')
Use backticks to escape column and table name.
Use quotes to limit strings.
Use nothing to limit numbers.
Don't use brackets at all in MySQL.
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,'%');
I've been trying to write this simple mysql statement to check if a table exists:
IF object_id('carpool'.'users', 'U') is not null THEN
PRINT 'Present!'
ELSE
PRINT 'Not accounted for'
END IF
'carpool' is my schema and 'users' is a table
My sql version is 5.1.52-community
I keep getting this error:
Error Code: 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 'IF' at line 1 0.000 sec
I've tried several syntaxes such as IF BEGIN END ELSE BEGIN END to no avail.
any ideas?
It looks like you're trying to do a form of T-SQL, which MySQL does not support.
There is no print function, nor object_id. There is a form of IF function that can be used in a SELECT statement:
SELECT IF(1 = 1, 'present', 'not');
Are you reading MSSQL doco instead of MySQL doco? MSSQL supports these functions.
Depending on what you are after, you might want to use something like this:
SELECT
CASE WHEN EXISTS (SELECT NULL
FROM information_schema.tables
WHERE table_schema = 'carpool' AND table_name = 'users')
THEN 'Present'
ELSE 'Not accounted for' END;
This will check if table users exists in carpool schema. Please see example here.
Every statement must be terminated by semicolon.
Like this
IF object_id('carpool'.'users', 'U') is not null THEN
PRINT 'Present!';
ELSE
PRINT 'Not accounted for';
END IF;