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;
Related
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
I am trying to write a Mysql query . I am getting some error on WITH clause of select function .
This is the sample query :
CREATE TRIGGER SAMPLE BEFORE INSERT ON SERVER
FOR EACH ROW
BEGIN
IF ((SELECT COUNT(S.ID) FROM SERVER S WITH UR) >= (SELECT L.SERVERS FROM SAMPLE L WHERE L.ID = 1 LIMIT 1)) THEN
SIGNAL SQLSTATE '73550' SET MESSAGE_TEXT='+ID';
END IF;
END;
and i am getting the error :
right syntax to use near 'UR) >= (SELECT L.SERVERS FROM SAMPLE L WHERE L.ID = 1 LIMIT 1))
Is WITH clause doesn't support in Mysql ? Or any other syntax should i use ? Any suggestion would helpful .
update :
Also i am using another query :
CREATE TRIGGER SAMPLE_TRIGGER NO CASCADE BEFORE INSERT ON TABLE_1
FOR EACH ROW
BEGIN
SET NEW.RID = (SELECT ID FROM TABLE_2 WHERE ACTIVE=0 FETCH FIRST 1 ROWS ONLY WITH RS USE AND KEEP EXCLUSIVE LOCKS);
IF (NEW.RID IS NULL) THEN
SIGNAL SQLSTATE '73550' SET MESSAGE_TEXT='+ID';
END IF;
END;
getting another error
right syntax to use near 'LIMIT 1 WITH RS USE AND KEEP EXCLUSIVE LOCKS);
IF (NEW.RID IS NULL) THEN'
In DB2, with ur means "with uncommitted read". This is a locking mechanism in the database, as explained here.
If you are porting code to MySQL, I would not worry about this. So, just remove it:
CREATE TRIGGER SAMPLE BEFORE INSERT ON SERVER
FOR EACH ROW
BEGIN
IF ((SELECT COUNT(S.ID) FROM SERVER S) >= (SELECT L.SERVERS FROM SAMPLE L WHERE L.ID = 1 LIMIT 1)) THEN
SIGNAL SQLSTATE '73550' SET MESSAGE_TEXT='+ID';
END IF;
END;
I have the following query that loops over chunks of data and copy it to another table. One of my goals for using label is to have a mechanism to exit from the loop once some condition are met:
SET #nth = 0;
SET #size = 1000 * 1;
SET #offset = #nth * #size;
SET #count = 7120;
lable1: LOOP
PREPARE select_stmt FROM
"INSERT INTO foo.`track_new` (
`...`
)
SELECT
`...`
FROM foo.`track` a
WHERE ...
ORDER BY ...
LIMIT ?, ?";
EXECUTE select_stmt USING #offset, #size;
IF #offset + #size > #count THEN LEAVE lable1;
END IF;
...
END LOOP;
but I am getting these errors (messages for successful queries were removed):
Query: lable1: LOOP PREPARE select_stmt FROM "INSERT INTO gps.`track_new` ( `id`, `unit_id`, `gps_time`, `added_when`, `latit...
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 'lable1: LOOP
PREPARE select_stmt FROM
"INSERT INTO gps.`track_new` (
`id`,
' at line 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
--------------------------------------------------
...
Query: IF #offset + #size > #count THEN leave lable1
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 #offset + #size > #count THEN leave lable1' at line 1
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
--------------------------------------------------
I have already checked for the syntax of label and it seems fine.
I have also checked for the syntax for loops and other control structures in mysql but I can't figure what's wrong.
EDIT:
I have noticed that the line EXECUTE select_stmt USING #offset, #size; gets executed but the Loop does not repeat afterwards.
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.
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 ;