The trigger successfully gets executed in the MSSM and in the front end application when the insert query is executed the application just hangs and stops responding. But when I put a hardcoded value instead of the last inserted value, it is working perfectly fine. I am not sure if my last inserted syntax is wrong or I am going wrong somewhere else.
What I am trying to do here is from one Database table, I am trying to get the last inserted unique id and insert that into another database's table with current timestamp.
Help is really appreciated in this.
USE [Dummy_Ibus]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[insert_veh_bus] ON [dbo].[tcbus]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE #last_inserted_unique_id NUMERIC(10,0);
SET #last_inserted_unique_id = 0;
select #last_inserted_unique_id = inserted.unique_id from inserted;
INSERT INTO bbus_sync.dbo.sync_history(unique_id,created_on) VALUES(#last_inserted_unique_id,CURRENT_TIMESTAMP)
END
Related
I'm trying to create a Trigger in Database Workbench (Lite, for MySQL) with this:
delimiter //
CREATE TRIGGER employeesTableInsert AFTER INSERT ON EMPLOYEES FOR EACH ROW
SET NEW.CREATED = CURRENT_TIMESTAMP; END IF;//
When I try to create the new Trigger, though, I get this err msg:
This seems odd; I look at the DDL tab in Database Workbench, and it has this:
Why is it doubling and mangling my Trigger code there in the (read-only) DDL?
As shown at the outset, my Trigger code only contains what is shown above:
So is the problem with my code, or is it because the DDL tab is confused? Or is my code CAUSING the DDL tab to become confused, or what?
If you want to update only the value you need following.
you can't use SET without a BEGIN and END, it is not a query.
I am wondering why there is a END IF in your code
Edit i also changed your TRIGGER to BEFORE INSERT, because on there you can change the values of a NEW column (see Bill Karwin comment)
And actually you don't need a trigger see manual
DELIMITER //
CREATE TRIGGER employeesTableInsert BEFORE INSERT ON EMPLOYEES
FOR EACH ROW
BEGIN
SET NEW.CREATED = CURRENT_TIMESTAMP;
END//
I am trying to set-up a trigger that will essentially archive information based on when a certain column is updated. What this should be doing is moving schedule_id and login from the schedule table to the schedule_archive table every time the term_date_schedule column is changed. However, that isn't happening. I am extremely new to MySQL, so I am probably missing something obvious.
CREATE DEFINER=`user`#`%` TRIGGER employee_term
AFTER UPDATE ON schedule
FOR EACH ROW
BEGIN
IF NEW.term_date_schedule <=> OLD.term_date_schedule THEN
INSERT INTO schedule_archive(schedule_id, login) VALUES(old.schedule_id, old.login);
END IF;
END
What is <=>? Use <> instead.
Does anyone here can suggest a way to track database changes that were made through wordpress? For example I add a menu in wordpress, how can I keep track of this changes in database?
I use mysql
I tried searching and I only find toad and mysqldiff but still no luck. I also tried activating tracking for mysql but it only records changes that were made through phpmyadmin
You can create a trigger function in your mysql database. This function could copy the row before any UPDATE or INSERT statement into a separate table.
http://dev.mysql.com/doc/refman/5.7/en/create-trigger.html
BUT! Write a small IF check into your procedure so that you can disable it. Otherwise you will have a hard time when importing data through sql scripts during development.
A sample:
DROP TRIGGER IF EXISTS `backup`;
DELIMITER //
CREATE TRIGGER `backup`
BEFORE UPDATE
ON `sourceTable` FOR EACH ROW
BEGIN
IF #disableTriggers <> 1 THEN
INSERT INTO `backupTable` (col1,col2,col3) VALUES (OLD.col1,OLD.col2,OLD.col3)
END IF;
END;
//
DELIMITER ;
How do you guys upgrade your game database?I actually mean add new column to your table, say we got a playerrole table which changes frequently.For some reason,I don't want to login to the database server to upgrade it.I want to write a script file to upgrade it when the game server start.Well, the patch script file looks something like the following one:
drop PROCEDURE IF EXISTS dbpatch;
DELIMITER ;;
create PROCEDURE dbpatch()
declare exit HANDLER for SQLEXCEPTION ROLLBACK;
BEGIN
start TRANSACTION;
// sql sentence
update version set v = versionnum;
commit;
end;;
DELIMITER;
call dbpatch();
drop PROCEDURE if EXISTS dbpatch;
The problem is that alter table statements which I put in dbpatch will end current active transaction and implicit commit according to mysql doc(https://dev.mysql.com/doc/refman/5.0/en/implicit-commit.html),this might cause partial success and could not be rolled back.For example, I got a table A and I want to add three new columns 'a', 'b', 'c' to this table,but table A already got column 'c'.The patch process will be terminated by sqlexception but column 'a' and 'b' will be added to talbe A.I hope it could be rolled back rather than partial success.Then I googled it, there are some ways to work around,but they tend to create a temporary table to hack it.
This way did not work for me,because I got some records in table,if I do it this way,I have to move records back and forth which is more error-prone.Any idea how to solve it?Any help will be appreciated.Our current mysql version is 5.5.27.
I would like to copy the data after insert from one table to another table in another db on the same instance of the server. I am using Microsoft SQL Server 2008 Enterprise.
I thought that the easiest way it will be using a trigger but I have problems with this.
This is my code:
CREATE TRIGGER [DB1].[dbo].[myTrigger]
ON [DB1].[dbo].[myTable]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [DB2].[dbo].[tempTable] (L_myTable_ID, L_Mode, C_Name)
SELECT (L_ID, L_Mode, C_Name) FROM INSERTED
END
Any suggestions?