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?
Related
We create a sample database, but in the creation we set our identities to specific values. To turn on Identity Insert we use
context.Database.ExecuteSqlCommand ( "SET IDENTITY_INSERT dbo.Tool ON;" );
we then call
context.SaveChanges ();
and then execute another sql command to turn Identity Insert off.
When we Add the entity is there a way to insert Identity Insert ON SQL command as text before the ADD and then to insert Identity Insert OFF SQL command as text after the Add.
I picture SaveChanges as generating the SQL text that will do the insert, this is why I am wondering if there is a way to insert SQL commands.
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
I am very new to SQL and sorry for asking this is very primary knowledge. My issue as follow.
I have a table called Group and minimumVal and maximumVal are two columns. minimumVal always should be less than maximumVal. I want to check this before inserting a new record to the table. If this condition failed data should not inserted into the DB.
So as per my understanding I though of having a trigger but have no idea how to write this.
This is what I write so far;
CREATE TRIGGER tr_Group
ON Table_Group
for INSERT
AS
????
Please advise me.
I recommend you use check constraints for this:
ALTER TABLE dbo.Table_Group ADD CONSTRAINT CK_Group
CHECK (minimumVal < maximumVal)
If you still want to use triggers, then you need an INSTEAD OF trigger:
CREATE TRIGGER tr_Group ON Table_Group
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Table_Group
SELECT *
FROM INSERTED
WHERE minimumVal < maximumVal
END
I'm trying to insert rows into a table via a trigger or stored procedure without writing any data to the binary log. Is this possible? I know that for normal connections you can set SQL_LOG_BIN=0 to disable binary logging for the connection, but I haven't been able to get that to work for triggers. If there's a way to do this with a federated engine table, that would also be OK.
edit:
I can almost accomplish this via a stored procedure:
CREATE PROCEDURE nolog_insert(`id` INT, `data` blob)
BEGIN
SET SESSION SQL_LOG_BIN = 0;
INSERT INTO `table` (`id`, `data`) VALUES (id, data);
SET SESSION SQL_LOG_BIN = 1;
END
I insert a record by calling the procedure from the mysql prompt:
call nolog_insert(50, 'notlogged');
As expected, the record (50, 'notlogged') is inserted into the table, but is not written to the binary log.
However, I want to run this procedure from a trigger. When using a trigger as follows:
create trigger my_trigger before insert on blackhole_table for each row call nolog_insert(new.id, new.data);
The data is both inserted to the table and written to the binary log.
If you run statement based replication triggers are executed on both the master and the slave but not replicated. Perhaps that could solve your problem.
Other than that, it's not allowed to change sql_log_bin inside a transaction so I would say that there is no good way to have the effects of a trigger not replicated when using row based replication.
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