My DBA set up many events that run SQL code thousands of lines long. Now that he is gone how do I change the EVENT DO statement when the code that is run is 1000s of lines?
I have tried doing it in workbench, it just runs the 1000s of lines of code
I have tied PHP PDO to run an ALTER EVENT 'name'
DO :placeholder
I have tried mysqli
all with standard bind errors. I have been able to insert the code into a text field in another table so I do know the php PDO works and was thinking is there a SQL comand to copy this feild into the event?
you can do between the backticks very much, but it is not recomended
$sql = "ALTER EVENT myschema.`my | event`
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;";
ALTER EVETNS isn't supported by Prepared statements with parmeters, you can only use strung replacement. but there you must you backticks for columns and apostrophes for strings
$sql = "ALTER EVENT myschema.`$myeventt`
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL $mytime HOUR
DO
UPDATE myschema.`$mytable` SET `$mycol` = `$mycol` + $myaddnumber,`$mycol2` = '$mystrimng';";
this you can use with pdo or mysqli
And php would repace the variables
The solution I found that works is:
DROP EVENT IF EXISTS databasename.processes_name;
DELIMITER $$
CREATE EVENT IF NOT EXISTS databasename.processes_name;
ON SCHEDULE EVERY 1 DAY STARTS '2018-02-05 08:00:00'
ON COMPLETION PRESERVE ENABLE
DO BEGIN
*thousands of lines of SQL code *
END; $$;
that is it
I was missing the BEGIN and END part
I'm trying to create an sql event with the following code
$query= "CREATE EVENT $this->eventId ON SCHEDULE AT CURRENT_TIMESTAMP + interval 1 minute ON COMPLETION NOT PRESERVE ENABLE DO UPDATE seats set seatStatus='free' where seatStatus='pending' and idSeat=(select idSeat from seats where seatNumber=$this->seatNumber AND idType=$this->seatType)";
it does not give any error code while executing it, but it won't create the event
I tried the bindParam method aswell,to pass the parameters to the query , it did not help
Actually I use PHP to insert some value into SQL server database table that contain one trigger that i have already created.
any error detected while the execution but I don't find the row.
But if I delete my trigger and I run the program again the row is inserted correctly.
Please help me.
Trigger code :
CREATE TRIGGER test6
ON dbo.test
AFTER INSERT
AS
BEGIN
Select * from dbo.tabletest
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
END
GO
I have a mysql table which has a trigger attached, that logs changes in this table to a second one
CREATE TRIGGER log_table BEFORE UPDATE ON table1
FOR EACH ROW BEGIN
INSERT INTO log_table(filed) VALUES(NEW.field);
END;
//
Now if I perform an INSERT INTO table1 from PHP an call mysqli_insert_id() afterwards.
Would that return the new ID in table1? Or the new ID in log_table?
LAST_INSERT_ID() called after the INSERT statement will return the new ID in table1 (of the INSERT statement), not the one in the log_table (of the trigger).
This is documented in the manual section of LAST_INSERT_ID:
Within the body of a stored routine (procedure or function) or a
trigger, the value of LAST_INSERT_ID() changes the same way as for
statements executed outside the body of these kinds of objects. The
effect of a stored routine or trigger upon the value of
LAST_INSERT_ID() that is seen by following statements depends on the
kind of routine:
If a stored procedure executes statements that change the value of
LAST_INSERT_ID(), the changed value is seen by statements that follow
the procedure call.
For stored functions and triggers that change the value, the value is
restored when the function or trigger ends, so following statements
will not see a changed value.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Access Auto-Increment Value During INSERT INTO Statement
I would like to generate the slug for my URLs upon creating the pages to store in the database. The slug should be title-id (e.g titlename-234). I already have the function to strip the title but how can i get the id before inserting the record?
Many thanks in advance
You should create a trigger, something like that:
CREATE TRIGGER trigger_name BEFORE INSERT ON table
FOR EACH ROW SET slug = CONCAT(NEW.title, "-", NEW.id);
I'm not sure you'll be able to access the ID column before its written on the database (unless, of course, you generate your IDs yourself, not using the autoincrement).
If you're using your DB's autoincrement (and you should be), try creating a trigger AFTER INSERT, and updating your row in your trigger. That way, even though you're updating it AFTER your insert, it'll still run before you can run any other queries (like a SELECT).
HERE is the documentation on triggers.
I was wrong. Apparently, you can't update a table you're inserting into (using an AFTER INSERT triger), it'll throw an exception. So, two possible ways to do it using SQL alone:
The ugly way:
DELIMITER $$
CREATE TRIGGER `create_slug` BEFORE INSERT
ON `events`
FOR EACH ROW
BEGIN
SET new.id = (SELECT MAX(id) FROM events) + 1;
SET new.slug = CONCAT(new.menu_name, "-", new.id);
END$$
DELIMITER ;
It overrides your database's AUTOINCREMENT. Really, don't do that.
Or, you can create a procedure:
DELIMITER $$
CREATE PROCEDURE insert_event(MenuName VARCHAR(30), Content TEXT, PhotoName TEXT)
BEGIN
INSERT INTO events (menu_name, content, photo_name) VALUES (MenuName, Content, PhotoName);
SET #id = LAST_INSERT_ID();
UPDATE events SET slug = CONCAT(menu_name, "-", #id) WHERE id = #id;
END$$
DELIMITER ;
And, instead of calling (as you were calling):
$query = mysql_query("INSERT INTO events (menu_name, content, photo_name) VALUES ('{$menu_name}', '{$content}', '{$photo_name}');", $connection);
just call
$result = mysql_query("CALL insert_event('{$menu_name}', '{$content}', '{$photo_name}');",$connection );
Again, I'll strongly advice against using mysql_query. It's outdated, discontinued and unsafe. You should check out mysqli or PDO.