i've been using pl/sql for a while.. And i'm bit confused about the variables in mysql procedure thing.
I'm trying to create a procedure, and I can't get it to compile :(
Error i got:
Heres the full code
CREATE DEFINER = CURRENT_USER PROCEDURE `NewProc`()
BEGIN
SET #city = FLOOR(RAND() * 5);
SET #uid = 3948;
SET #alive = (select alive from users where id = #uid);
SET #thread_id = 2950;
set #post_content = "AAAAOOOAAA! Jeg er i ";
if (#alive = 1) THEN
# Move NPC over to a random city
UPDATE users set city = #city where id = #uid;
# post a post on the forum thread
INSERT INTO forum_posts (
author,
thread,
creationdate,
content,
) values (
#uid,
#thread_id,
UNIX_TIMESTAMP(),
#post_content
);
END IF;
END;;
Anyone of you geniusens that see what I've done wrong :)?
Related
I had tried to add or remove the '#' before variables or params but nothing happened.
QUERY
start transaction;
set #recordClient = (select ClientId from by_test_db1.recordcd where SN = 'abc' );
set #logClient = (select ClientId from by_test_db1.log where SN = 'abc' );
select concat(#recordClient,#logClient);
commit;
SP
delimiter $$
create procedure TEST(newSN varchar(50))
begin
start transaction;
set #recordClient = (select ClientId from by_test_db1.recordcd where SN = newSN );
set #logClient = (select ClientId from by_test_db1.log where SN = newSN );
select concat(#recordClient,#logClient);
commit;
end $$
delimiter ;
call TEST('abc');
MySQL version 5.7
Through there are 100 million rows in the recordcd table ,the QUERY just ran fast and well, but the SP was running so slowly that it timed out and reported an error
Error Code: 2013. Lost connection to MySQL server during query
I tried many ways but none of them worked, I don't know why there is such a ridiculous situation, I don't even know how to search the answer to this situation.
Add indexes
INDEX(SN) -- on both tables
Simplify query
SELECT CONCAT(
( select ClientId from by_test_db1.recordcd where SN = 'abc' ),
( select ClientId from by_test_db1.log where SN = 'abc' ) );
Error: [sql] Couldn't insert SQL accounting START record - Can't update table 'useraccounting' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
We get the above error frequently after the user authenticated and it take more than 1 min for the user to appear in online table. Any idea about the cause and solutions?
Thanks
Update:
Below the trigger that is used:
CREATE DEFINER=`mysql`#`%` TRIGGER `radacct_after_update` AFTER UPDATE ON `radacct` FOR EACH ROW BEGIN
if (NEW.acctoutputoctets != OLD.acctoutputoctets)
THEN
UPDATE useraccounting SET tmpQuta = tmpQuta + (New.acctoutputoctets - OLD.acctoutputoctets)
WHERE EXISTS (SELECT * FROM users WHERE users.userable_id = useraccounting.client_id AND users.userable_type = 'App\\Models\\Client' AND (users.username = New.username));
END IF;
END
UPDATE2: we have created 2 triggers. One on INSERT and another one on UPDATE.
After INSERT on radacct:
BEGIN
UPDATE useraccounting SET tmpQuta = New.acctoutputoctets
WHERE EXISTS (SELECT * FROM users WHERE users.userable_id = useraccounting.client_id AND users.userable_type = 'App\\Models\\Client' AND (users.username = New.username));
END
after UPDATE on radacct:
BEGIN
if (NEW.acctoutputoctets != OLD.acctoutputoctets)
THEN
UPDATE useraccounting SET tmpQuta = tmpQuta + (New.acctoutputoctets - OLD.acctoutputoctets)
WHERE EXISTS (SELECT * FROM users WHERE users.userable_id = useraccounting.client_id AND users.userable_type = 'App\\Models\\Client' AND (users.username = New.username));
END IF;
END
I'm attempting to move data from one table to another, as part of a restructure to the database architecture. I'm using PHPMyAdmin and MySQL to do so.
The SQL is meant to, for each emergency_contacts.id, move e_c.id and e_c.activity_id to activities_emergency_contacts, where the pair will form a composite key to link a contact with an activity.
The following SQL returns an error:
CREATE PROCEDURE dowhile()
BEGIN
SET #cid = (SELECT MIN(`id`) FROM `emergency_contacts`);
SET #aid = (SELECT `activity_id` FROM `emergency_contacts` WHERE `id` = #cid);
WHILE #cid IS NOT NULL DO
INSERT INTO activities_emergency_contacts (activity_id, contact_id)
VALUES (#aid, #cid);
SET #cid = (SELECT MIN(id) FROM emergency_contacts WHERE #cid < id);
SET #aid = (SELECT activity_id FROM emergency_contacts WHERE id = #cid);
END WHILE;
END;
CALL dowhile();
SQL query:
CREATE PROCEDURE dowhile() BEGIN SET #cid = (SELECT MIN(id) FROM
emergency_contacts)
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 '' at line 3
I have searched the MySQL specific documentation to try and find any issues with my SET, WHILE, BEGIN/END, INSERT, CREATE - just about every line. I'm unsure how to proceed. Any advice would be greatly appreciated.
EDIT: I missed a closing bracket inside the WHILE on SELECT MIN (id), however I've fixed this and am still getting the exact same issue.
EDIT: The issue was that I was not using DELIMITER. Correct SQL:
DELIMITER $$
CREATE PROCEDURE dowhile()
BEGIN
SET #cid = (SELECT MIN(`id`) FROM `emergency_contacts`);
SET #aid = (SELECT `activity_id` FROM `emergency_contacts` WHERE `id` = #cid);
WHILE #cid IS NOT NULL DO
INSERT INTO activities_emergency_contacts (activity_id, contact_id) VALUES (#aid, #cid);
SET #cid = (SELECT MIN(id) FROM emergency_contacts WHERE #cid < id);
SET #aid = (SELECT activity_id FROM emergency_contacts WHERE id = #cid);
END WHILE;
END$$
DELIMITER ;
CALL dowhile();
I am working for a little film database with php and mysql. And i got a problem when I want to create a stored procedure, that the local variable don't work in the case i want to use it.
delimiter //
### showFilm###
create procedure showFilm( in id INTEGER )
BEGIN
DECLARE tmp_Value varchar(50);
IF ( id = 0) THEN SET tmp_Value = "";
ELSE SET tmp_Value = concat('where = ', id);
END IF;
select f.Titel, f.Jahr, f.Bewertung, f.altersfreigabe, f.Beschreibung, f.Trailer, f.Dateipfad, f.Titelbild, f.Speichermedium, r.Vorname, r.Nachname, r.Land, r.Bild
from film as f
inner join regisseur as r
on r.idRegisseur = f.idFilm tmp_Value;
END;
//
I have created a trigger on a table, and when I try to insert data into it I get MySql error "Error Code: 1054 'unknown column license_key in field list'"
The INSERT I try to do is this:
INSERT INTO LOAD_MASTER(license_key, state, total_pageviews, total_visitors, max_visitors, max_pageviews, ip, secret, read_time, url)
VALUES ("order55555hytgtrfderfredfredfredftyu8ikloi98nhygt6", "preparing", 400, 1000, 200, 400, "202,2,35,36", "Hemmeligheden", 120, "http://google.dk");
The Trigger I have created is supposed to check the TABLE LOAD_STATS for the presence of the IP entered in the LOAD_MASTER table, and then based on the results change a value in another table called LICENSE
My trigger is here:
DELIMITER //
CREATE TRIGGER ip_check AFTER INSERT ON LOAD_MASTER FOR EACH ROW
BEGIN
DECLARE MK varchar(50);
DECLARE MIP varchar(15);
DECLARE LID int(6);
SET MK = license_key;
SET MIP = ip;
SET LID = (
SELECT l.license_id
FROM license_keys k, license l
WHERE l.license_id = k.license_id
AND k.license_key = MK
);
IF (SELECT COUNT(DISTICT(master_ip)) FROM LOAD_STATS WHERE master_ip = MIP AND master_key = MK ) > 3 THEN
UPDATE LICENSE
SET STATE = 0
WHERE license_id = LID;
END IF;
END
//
DELIMITER ;
Any help on why I get this error would be much appriciated.
-Dan
In these lines
SET MK = license_key;
SET MIP = ip;
you probably meant to address NEW.license_key and NEW.ip respectively.
IMHO you can make more succinct. Try this
DELIMITER //
CREATE TRIGGER ip_check
AFTER INSERT ON load_master
FOR EACH ROW
BEGIN
IF (SELECT COUNT(DISTICT(master_ip))
FROM load_stats
WHERE master_ip = NEW.ip
AND master_key = NEW.license_key ) > 3 THEN
UPDATE license
SET state = 0
WHERE license_id =
(
SELECT l.license_id
FROM license_keys k JOIN license l
ON l.license_id = k.license_id
AND k.license_key = NEW.license_key
); -- make sure that this subquery returns only ONE record
END IF;
END //
DELIMITER ;
There is some room for improvement rewriting an UPDATE with JOIN instead of using a subquery