So I needed to convert a MySQL database to a SQL Server 2008 R2 database. To do this I used Microsoft's new SSMA MySQL tool. Everything was converted fine except for the triggers, it kept crashing when converting them.
First MySQL trigger:
CREATE
DEFINER=`root`#`localhost`
TRIGGER `dbo`.`table_A_trig`
BEFORE INSERT ON `dbo`.`table_A`
FOR EACH ROW
BEGIN
/* Logic here */
END
Second MySQL trigger:
CREATE
DEFINER=`root`#`localhost`
TRIGGER `dbo`.`table_A_trig_Update`
AFTER UPDATE ON `dbo`.`table_A`
FOR EACH ROW
BEGIN
/* Logic Here */
END
How should I rewrite them to be able to create them in SQL Server 2008?
Thanks guys!
A couple of differences to point out.
SQL Server doesn't have a "before" trigger, so you'd have to convert the first as an INSTEAD OF trigger and manually perform the insert operation.
SQL Server triggers use the special INSERTED (equivalent to NEW in MySQL) and DELETED (equivalent to OLD in MySQL) tables.
So your CREATE TRIGGER syntax would look something like:
CREATE TRIGGER dbo.table_A_trig
ON dbo.table_A INSTEAD OF INSERT
AS
BEGIN
/* logic here */
/* Manually perform the insert operation */
INSERT INTO dbo.table_A
(column_list)
SELECT column_list
FROM INSERTED
END
CREATE TRIGGER dbo.table_A_trig_Update
ON dbo.table_A FOR UPDATE
AS
BEGIN
/* logic here */
END
Related
Working on a security DDL trigger for a database and want to restrict non-admins from creating/dropping database, I have created this trigger and put it below all the other code (create tables etc) in the database script.
CREATE TRIGGER alert_table
ON DATABASE
FOR CREATE_TABLE, DROP_TABLE, ALTER_TABLE
AS
BEGIN
IF IS_MEMBER ('admin') = 0
BEGIN
PRINT 'Please contact your Database Admin'
ROLLBACK TRANSACTION;
END
END
GO
CREATE
Note:
USER 'admin'#'localhost'
It says "ON is not valid at that position, expecting: BEFORE or AFTER", and something similar for "END". Thanks!
You followed a tutorial for Microsoft SQL Server, commonly abbreviated as mssql server, while you are using mysql.
Mssql server has DDL triggers which syntax follows the one in your question.
Mysql's create trigger syntax is different and does not have DDL triggers at all, so you cannot use triggers to handle table creation events.
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 ;
Problem is that, I want a trigger that deletes old rows in the same table that the new rows are being inserted into.
MsSQL and oracle can do this,
but looks like mySQL can't,
It allows the trigger to be created, but when it runs it gives the error
"can't update table "tbl" in stored procedure or function/trigger
because it is already used by statemtent whicgh invoked this stored
procedure or function/trigger"
Any work around for this?
Is it planned in future releases?
I have in my database all tables with filed name GHOST, so when GHOST is TRUE this row is like DELETED. So if You want change row after insert maybe use like this:
CREATE TRIGGER my_trigg
BEFORE INSERT ON table
FOR EACH ROW
BEGIN
SET NEW.ghost = TRUE;
END
$$
DELIMITER ;
I have a mysql table which on any change (i.e. insert, update and delete) I need to run the relevant trigger code.
Do I need to create three different triggers or is there a syntax for just one.
Using mysql 5.1
Three triggers may perform better and AFAIK - there is no possibility to create multi-action trigger in MySQL, but I hope the syntax for one trigger is:
CREATE TRIGGER Name AFTER INSERT ON Table
FOR EACH ROW
begin
...
END
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?