MySQL Trigger Error using variables - mysql

I'm trying to get the following code to run in a MySQL Trigger but I get error 1064 when I try to save it.
SET #ma = (SELECT modem_alias FROM `play`.`veh` WHERE meid = new.org_a LIMIT 1);
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, SELECT #ma);
The trigger is set to run on 'org' table after an INSERT

Don't use SELECT in the INSERT, just the variable:
INSERT INTO `play`.`des` (`indx`, `des_a`, `des_b`) VALUES (NULL, new.org_a, #ma);
You can also combine the two queries so you don't need a variable:
INSERT INTO play.des (indx, des_a, des_b)
SELECT NULL, new.org_a, modem_alias
FROM play.veh
WHERE meid = new.org_a
LIMIT 1

Your values is wrong: You can't select there, just use the variable itself:
INSERT ... VALUES (..., #ma);

Related

Inserting Multiple Rows in MySQL Easily?

I'm having a bit of trouble. I need to award an item to users on our site, but I don't want to manually fill in the numbers one by one. Is there a way to set the SQL query to INSERT INTO from UID 9 to 5430 without having to create multiple lines? Here's my example.
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, 'x', '3626', '1', '1592596732', 'NotBanned')
I'm trying to have the "x" be a number, but to have MYSQL generate multiple numbers from 9 to 5430 without having to generate multiple numbers/code all at once. So something like:
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, '9 - 5430', '3626', '1', '1592596732', 'NotBanned')
The 9 - 5430 is where the issue is. I want to award the item to everyone who has their number between the number 9 and 5430.
Help appreciated - thanks.
You can use stored procedure in mysql to do this; inside your stored procedure you can use a loop to insert multiple entries. I've provided an example below.
Procedure can be implemented like the code shown here:
delimiter $$
create procedure fill_rows(in start_index int,in termination_point int)
begin
while start_index <= termination_point do
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, start_index, '3626', '1', '1592596732', 'NotBanned');
set start_index := start_index + 1;
end while;
end $$
delimiter ;
Now whenever you want to insert uid let's say from range x to y. Assume x = 10 and y = 1000 then you can simply insert this records using this one time procedure call like:
call fill_row(10, 1000);
This call will insert 990 new rows with uid values 10, 11, 12 ...1000.
Hope this may help you!

Insert MySQL Query with use of concat

Im trying to insert a query to my database using a name in a variable and also using a name in the values. So i want to make it in to 1 name.
Here is an example
SET #name = "This is my";
INSERT INTO mytable (id, name)
VALUES (1, #name + "Test Query");
So, This query should then insert a row with id 1 and name "This is my Test Query" But it gives me an error. Truncated incorrect DOUBLE value: 'Test Query'
You can't use #name + "Test Query" syntax instead you shoud use concat() mysql function see here for more info:http://www.w3resource.com/mysql/string-functions/mysql-concat-function.php
SET #name = "This is my";
INSERT INTO `names` (id, name)
VALUES (1, concat(#name, 'Test Query'));
also make sure that your id is not autoincreament if it is then no need to pass id number in your query like this
INSERT INTO `names` (name)
VALUES (concat(#name, 'Test Query'));
i found no error in sql.. it executed without error..
DECLARE #name as varchar(100)
DECLARE #mytable as table ( id int, [name] varchar(100))
SET #name = 'This is my';
INSERT INTO #mytable (id, name)
VALUES (1, #name + 'Test Query');
Select * from #mytable

MySQL: Insert with conditional

I must perform the following situation down, but when you run got the error:
SQL 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 'INTO produto_seriais(serial_id) VALUES( SELECT id
FROM seriais WHERE serial =' at line 5
SELECT CASE WHEN (
SELECT COUNT(id) FROM seriais WHERE serial = '2020'
) > 1
THEN
(INSERT INTO produto_seriais(serial_id) VALUES(
SELECT id FROM seriais WHERE serial = '2020'
))
ELSE (
INSERT INTO seriais (serial) VALUE('2020');
SET #last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO produto_seriais (serial_id) VALUES (#last_id_in_table1);
)
END;
The case is as follows:
I'll get in "serial" table by serial "X". If it already exists, unless your ID in the "produto_seriais" table. If there is (serial), I will save the same, recover your ID and save to "produto_seriais". Any suggestions for how to do this?
Important Note: This routine will run will be thousands of times each execution (10,000 or more, depending on the quantity of serial).
P.s.: Sorry for my bad english.
Try a stored procedure
DELIMITER //
CREATE PROCEDURE sp_produto_seriais(IN `p_serial_id`)
IF EXISTS (SELECT * FROM seriais WHERE serial = p_serial_id )
BEGIN
INSERT INTO produto_seriais (serial_id)
SELECT id
FROM seriais
WHERE serial = p_serial_id
END
ELSE
BEGIN
INSERT INTO seriais (serial) VALUE(p_serial_id);
INSERT INTO produto_seriais (serial_id) VALUES (LAST_INSERT_ID());
END //
DELIMITER ;
usage:
CALL sp_produto_seriais('2020')
You could use the if exists..else statement.
If exists (select * from ....)
Begin
Insert into ... Select id from table
End
Else
Begin
Insert..
End
Please fill .... with your statements. You could use the link here to convert it for MySQL.
Convert if exists for MySQL

Creating variables in MySQL Triggers

Please have a look at the below SQL Query.
DELIMITER $$
CREATE TRIGGER `Portfolio_AINS` AFTER INSERT ON `Portfolio` FOR EACH ROW
INSERT INTO Initial_Fees (idPortfolio, Current_Time_Stamp, Initial_Gross_Fee, Initial_Monetary_Fee)
VALUES (
New.idPortfolio,
current_timestamp,
(New.Invest_Amount*(New.Initial_Gross_Fee/100)),
(New.Invest_Amount*(New.Initial_Gross_Fee/100))*(New.Initial_Company_Fee/100)
)
In here, the Initial_Monetary_Fee = Initial_Gross_Fee * (New.Initial_Company_Fee/100)
But I have written it (New.Invest_Amount*(New.Initial_Gross_Fee/100))*(New.Initial_Company_Fee/100)
Instead of that, I would like to create variables and assign the values to them, so I can avoid such long calculations. For an example, something like below.
InitialGrossFee = (New.Invest_Amount*(New.Initial_Gross_Fee/100))
IntialMonetaryFee = InitialGrossFee * (New.Initial_Company_Fee/100)
So, how can I create such variables and store values in side MySQL triggers? I prefer to have some explanation as well.
If you need multiple statements within in a trigger you must use BEGIN .... END.
Then you can assign values to uservars as usual:
CREATE TRIGGER `Portfolio_AINS` AFTER INSERT ON `Portfolio`
FOR EACH ROW
BEGIN
SET #grossfee := New.Invest_Amount*(New.Initial_Gross_Fee/100);
INSERT INTO Initial_Fees (idPortfolio, Current_Time_Stamp, Initial_Gross_Fee, Initial_Monetary_Fee)
VALUES (
New.idPortfolio,
current_timestamp,
#grossfee,
#grossfee *(New.Initial_Company_Fee/100)
);
END$$

Error 1111: mysql on trigger from insert

I am trying to understand why this trigger keeps giving me an error about invalid use of grouped function when i try to run a basin insert statement to test this out.
I have tried working with this to figure out what i am doing wrong but the error just remains the same. Error 1111
DROP TRIGGER a_num;
DELIMITER //
CREATE TRIGGER a_num BEFORE INSERT ON test_a
FOR EACH ROW BEGIN
DECLARE last INT DEFAULT 0;
INSERT INTO test_b SET full_name = CONCAT_WS(' ', NEW.f_name, NEW.l_name);
SET last = COUNT(id);
UPDATE test_b SET number = CONCAT_WS('-', last, LEFT(NEW.f_name, 2), LEFT(NEW.f_name, 2)) WHERE id = last;
END;
//
Please don't mind the use or poor construction I quite a newb.
Thanks.
I think it should be -
DROP TRIGGER a_num;
DELIMITER //
CREATE TRIGGER a_num BEFORE INSERT ON test_a
FOR EACH ROW BEGIN
DECLARE last INT DEFAULT 0;
INSERT INTO test_b SET full_name = CONCAT_WS(' ', NEW.f_name, NEW.l_name);
SET last = LAST_INSERT_ID();
UPDATE test_b SET number = CONCAT_WS('-', last, LEFT(NEW.f_name, 2), LEFT(NEW.f_name, 2)) WHERE id = last;
END;
//
Can you provide the CREATE statement for test_a and the INSERT statement you're using?
In MySQL Workbench if you right click on test_a go to Copy to Clipboard..Create Statement, will send the table definition.
Is there a reason you're inserting and then updating the same record? Could you combine this into one insert?