How to use strcmp in if statement in mysql? - mysql

I have to compare two string and process based on the result of strcmp in my sql.
sample:
Set #login='USC00010';
set #user='USC00010';
select STRCMP(#login,#user);//returns 0
if(STRCMP(#login,#user)= 0)
THEN
//process1
else
//process2
end if;
and the above code throws 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(STRCMP(#login,#user)= 0)
Actual requirement:
if(//matches)
begin
if exists(select X from X )
begin
if exists(select x from x where y=a)
begin
update x
end
end
else//doesn't match
begin
//process2
end

Set #login='USC00010';
set #user='USC00010';
select if(STRCMP(#login,#user)= 0,"match","no-match") as str_compare

Related

I want to generate a varchar code with mysql

I want to generate a code with mysql something like 'B45'. the generation starts at 'A0' and ends in 'Y99'
, but somehow there is a problem, please I need help with my code
CREATE TRIGGER oeuvre_code BEFORE INSERT ON oeuvres
BEGIN
IF NOT EXISTS(select * from oeuvres) THEN
SET #code2 ='A0';
ELSE
SET #test = (select code from oeuvres LIMIT 1);
SET #char =(select left (#test,1));
SET #nbr = select substr(#test,2);
if #char!='Z' AND #nbr+0 <99 THEN
set #char2 = select (ASCII(#char)+1);
set #nbr2 = #nbr+1;</i>
END IF;
set #code2=#char2+#nbr2;
INSERT INTO oeuvres VALUES(NEW.Id,NEW.Toile,NEW.Description,NEW.Prix,NEW.Date,NEW.Etat,#code2);
END IF;
END;
Here is the problem I get:
Error
SQL query: Documentation
CREATE TRIGGER oeuvre_code BEFORE INSERT ON oeuvres
BEGIN
IF NOT EXISTS(select * from oeuvres) THEN
SET #code2 ='A0'
MySQL said: Documentation
#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 'BEGIN
IF NOT EXISTS(select * from oeuvres) THEN
SET #code2' at line 2
Picture of the problem I get

mysql trigger syntax error in maria db

I was trying to make one trigger , so that i can check for any negative insertion of age .
I tried below query but its showing below error .
CREATE TRIGGER agecheck
BEFORE INSERT ON people
FOR EACH ROW
BEGIN
(CASE WHEN people.age > 0 THEN people.age ELSE 0 END )
END
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 'CASE WHEN people.age > 0 THEN 1 ELSE 0 END )
END' at line 5
Am i doing anything wrong in the syntax?
The statement you have in your trigger is not a statement. It's just an expression. This is not a thing you can use as a statement by itself.
I'm just guessing at what you are trying to do: make sure age is not negative. You could probably do this by declaring the column as TINYINT UNSIGNED, but if you want to do it with a trigger:
FOR EACH ROW
BEGIN
SET NEW.age = CASE WHEN NEW.age > 0 THEN NEW.age ELSE 0 END;
END
In a trigger, always reference the columns of the respective triggered row with NEW.<columnname> or OLD.<columnname>.
Another expression that works just as well:
FOR EACH ROW
BEGIN
SET NEW.age = GREATEST(NEW.age, 0);
END
There's no specific advantage for this alternative, I'm just showing another use of an expression.

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is 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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

mySQL If statement

I am trying to make this If Statement work, but I can't seem to make it do what I want. If I do a select #result, It'll give me the value 0, then why doesn't the IF statement work?
SET #message = '((sometihng here))';
select LEFT(#message, 1) into #firstChar;
select STRCMP(#firstChar,'(') into #result;
IF (#result = 0) THEN
SET #message = 'true';
//more selects and cals here;
END IF;
select #message;
I should get true, but I don't it shows me an error:
SQL query: IF( #result =0 ) THEN SET #message = 'true';
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 'IF (#result = 0) THEN SET #message = 'true'' at line 1
try use function http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
SELECT IF(#result = 0, 'true', '((something here))') AS message
As Max Mara pointed out, that's a good work aroud. The reason the IF wasn't working is not because the syntax is incorrect, but because flow control functions like IF ... THEN are only valid inside of stored procedures or functions, All this thanks to #TehShrike
The IF .. THEN .. ELSE syntax in MySQL is only available for procedural code (stored precudures, functions, triggers..), but not for SELECT statements.
IF ELSE USED IN STORED PROCEDURE EXAMPLE BELOW
DELIMITER //
CREATE PROCEDURE NAME(IN Number INT)
BEGIN
IF roll= 1
THEN SELECT * FROM table1 WHERE id = roll;
ELSE
SELECT * FROM table2 WHERE id = 2;
END IF;
END //
DELIMITER ;

Stored procedures written in MySQL 5.5.8 don't work in 5.1

I have some stored procedures and a trigger that work great in MySQL 5.5.8 but for some reason don't work in 5.1. The error descriptions aren't enough for me to figure out the problem. Here is the code and the errors.
CREATE PROCEDURE `cg_getMatchingContent`(
MatchTerm VARCHAR(255),
MaxResults INT)
BEGIN
SELECT * FROM (
SELECT t.*, INSTR(t.`Title`,MatchTerm) as Pos
FROM cg_content t ) c
WHERE Pos>0 ORDER BY Pos LIMIT 0, MaxResults;
END
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 'MaxResults' at line 8
DELIMITER ;;
CREATE TRIGGER `cg`.`cg_content_UrlDup_ConstTrig`
BEFORE INSERT ON `cg`.`cg_content`
FOR EACH ROW
Begin
DECLARE errorString VARCHAR(500);
DECLARE insert_error CONDITION FOR SQLSTATE '99001';
IF new.Url = '' THEN
SET errorString = CONCAT('Url cannot be blank
Title: ' , new.Title);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
END if;
IF Exists(SELECT id FROM cg.cg_content WHERE Url=new.Url) THEN
SET errorString = CONCAT('Url is not unique
Title: ' , new.Title , '
Url: ' + new.Url);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
End if;
End ;;
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 'insert_error
SET MESSAGE_TEXT=errorString;END if;IF ' at line 10
From the docs:
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
5.1 does not support variables in LIMIT and OFFSET.
The second one is easy to figure, hard to fix. SIGNAL and RESIGNAL commands were introduced in MySQL 5.5. You can't convert it easily to 5.1. One way to do it, would be to run a query that errors. For example a SELECT from a non-existent table.