I want to generate a varchar code with mysql - 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

Related

Running the following query from my PhpMyadmin SQL tab

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#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 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

How to use strcmp in if statement in 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

Sql trigger's query

I am trying to create custom id for my table in the following format
2 random alphabets - 00MaxID
for e.g: AA-001
I have tried writing a query for it but it is not working, this is my first time writing a trigger also writing a complex query such as this.
UPDATED-2
the following query gives me an error near "SELECT count(cus_id) INTO #ct FROM customer;"
CREATE
TRIGGER `id_gen` BEFORE INSERT
ON `testdb`.`customer`
FOR EACH ROW BEGIN
SELECT count(cus_id) INTO #ct FROM customer;
IF #ct < 1000 THEN
SET #cs_id = LPAD(#ct+1, 3, 0 );
ELSE
SET #cs_id = #ct+1;
END IF;
SET NEW.cus_id = CONCAT(CHAR(FLOOR(65 + RAND() * 26),FLOOR(65 + RAND() * 26)),'-',#cs_id);
END;
Error
SQL query: Documentation
CREATE
TRIGGER `id_gen` BEFORE INSERT
ON `testdb`.`customer`
FOR EACH ROW BEGIN
SELECT count(cus_id) INTO #ct FROM customer
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 '' at line 6
Executing with Trigger Section
Answered by #Solarflare.
In the screenshot, you can see that phpmyadmin automatically added code including for each row, which is now twice in the statement (thus the error). Your own code will start with begin.
BEGIN
SELECT count(cus_id)+1 INTO #ct FROM customer;
IF #ct < 1000 THEN
SET #cs_id = LPAD(#ct, 3, 0 );
ELSE
SET #cs_id = #ct;
END IF;
SET NEW.cus_id = CONCAT(CHAR(FLOOR(65 + RAND() * 26),FLOOR(65 + RAND() * 26)),'-',#cs_id);
END;

Keep Getting a Syntax Error when Creating MySQL Trigger

I am trying to Create a Trigger that will fire AFTER Insert of a Record where I will see if there is other records similar to this Inserted Record (Same Date) and if so will update a column in the inserted Record. Once I complete this one I will also update it for AFTER Update as well. Any Help would be Greatly Appreciated.
CREATE
TRIGGER `INSERT_POSTDATEINDEX` AFTER INSERT
ON `zoomloca_listings-dev`.`listings_posts`
FOR EACH ROW
BEGIN
DECLARE vNewPostDateIndex INT;
DECLARE vLastPostDateIndex INT DEFAULT '0';
SET vNewPostDateIndex = '0';
SET vLastPostDateIndex = (SELECT POSTDATEINDEX FROM listings_posts WHERE date(POST_DATE) = date(NEW.POST_DATE) ORDER BY POSTDATEINDEX DESC LIMIT 1);
IF vLastPostDateIndex = '0' THEN
SET vNewPostDateIndex = '0';
ELSE
SET vNewPostDateIndex = vLastPostDateIndex + 1;
END IF;
Update `listings_posts` SET POSTDATEINDEX = vNewPostDateIndex where ID = New.ID;
END
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 6
The problem is that there is no TOP in MySQL. You have to use LIMIT instead. Besides, if you are not using mysql client, then you should remove DELIMITER since it is not a feature of the MySQL. Another thing is that to demarcate the end of an IF statement in MySQL, you should use END IF instead of ENDIF.

MySQL after update trigger - hidden syntax error

the following trigger cannot be created on my local testing db (MySQL 5.6) and I do not understand why.
DELIMITER //
CREATE TRIGGER write_status_history AFTER UPDATE ON request
FOR EACH ROW
BEGIN
DECLARE snew CHAR;
DECLARE sold CHAR;
DECLARE unew CHAR;
DECLARE uold CHAR;
-- Status change
IF NEW.status <> OLD.status THEN
SET unew = (SELECT username FROM ta_users WHERE id = NEW.ta_users_id);
SET snew = (SELECT name FROM status WHERE id = NEW.status);
SET sold = (SELECT name FROM status WHERE id = OLD.status);
INSERT INTO history (`request_id`, `content`)
VALUES(NEW.id, CONCAT('Statuswechsel von ', sold, ' nach ', snew, ' durchgeführt von ', user, '.'));
-- User change
ELSE IF NEW.ta_users_id <> OLD.ta_users_id THEN
SET unew = (SELECT username FROM ta_users WHERE id = NEW.ta_users_id);
SET uold = (SELECT username FROM ta_users WHERE id = OLD.ta_users_id);
INSERT INTO history (`request_id`, `content`)
VALUES(NEW.id, CONCAT('Bearbeiter gewechselt von ', uold, ' nach ', unew, '.'));
END IF;
END
//
DELIMITER ;
I get this error messsage indicating a syntax error if I attempt to launch the query:
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 '' at line 21.
MySQL Workbench, however, reports Syntax Error: missing 'if' when hovering over the last END.
Do you have an idea how to fix this syntax error? I already tried adding a semicolon to the last END, that didn't help - same error.
Mysql else-if would be as
elseif
When you give a space it will consider else and then another if and will expect the inner if to be closed.