Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to trigger after insert to new database tbl

Author  Topic 

micnie_2020
Posting Yak Master

232 Posts

Posted - 2013-07-25 : 02:51:53
Hi All,

I getting error:

IDENTITY_INSERT is already ON for table 'PRAF.dbo.tblBrand'. Cannot perform SET operation for table 'eAsset.dbo.tblBrand'.


Alter TRIGGER BrandTriggerINSERT ON tblBrand after INSERT AS

DECLARE @ID int, @Description nvarchar(max)
SET @ID = (SELECT BrandID FROM inserted)
SET @Description = (SELECT [Description] FROM inserted)

SET IDENTITY_INSERT PRAF.dbo.tblBrand ON

insert into Praf.dbo.tblBrand
(BrandID,[Description],TypeID)
select BrandID,Description,TypeID from eAsset.dbo.tblBrand
where BrandID=@ID

SET IDENTITY_INSERT PRAF.dbo.tblBrand OFF

GO


Then, i change to below code by unqoute the set identity_insert, i got error:
Violation of PRIMARY KEY constraint 'PK_tblBrand'. Cannot insert duplicate key in object 'dbo.tblBrand'.
The statement has been terminated.


Alter TRIGGER BrandTriggerINSERT ON tblBrand after INSERT AS

DECLARE @ID int, @Description nvarchar(max)
SET @ID = (SELECT BrandID FROM inserted)
SET @Description = (SELECT [Description] FROM inserted)

--SET IDENTITY_INSERT PRAF.dbo.tblBrand ON

insert into Praf.dbo.tblBrand
(BrandID,[Description],TypeID)
select BrandID,Description,TypeID from eAsset.dbo.tblBrand
where BrandID=@ID

--SET IDENTITY_INSERT PRAF.dbo.tblBrand OFF

GO


How can i handle this issue.

What actually i needed are:
Database: eAsset Tablename: tblBrand
trigger after insert success and duplicate the new insert record with exactly same BrandID to another database: PRAF, tablename: tblBrand.

Please advise.

Thank you.

Regards,
Micheale

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-25 : 04:02:32
you need to do it like this


Alter TRIGGER BrandTriggerINSERT ON tblBrand after INSERT AS
SET IDENTITY_INSERT PRAF.dbo.tblBrand ON

insert into Praf.dbo.tblBrand
(BrandID,[Description],TypeID)
select BrandID,Description,TypeID from inserted

SET IDENTITY_INSERT PRAF.dbo.tblBrand OFF

GO



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2013-07-25 : 04:11:07
Hi,

I got the same error:
IDENTITY_INSERT is already ON for table 'PRAF.dbo.tblBrand'. Cannot perform SET operation for table 'eAsset.dbo.tblBrand'.


Please advise.

Thank you.

Regards,
Micheale
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-25 : 04:32:09
quote:
Originally posted by micnie_2020

Hi,

I got the same error:
IDENTITY_INSERT is already ON for table 'PRAF.dbo.tblBrand'. Cannot perform SET operation for table 'eAsset.dbo.tblBrand'.


Please advise.

Thank you.

Regards,
Micheale


set it to off first before creating the trigger. then create the trigger

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2013-07-25 : 05:14:48
Hi,

I already set to off. Seem like cannot detect the database.dbo.

Any method to perform this? If same database and different table name it's work fine.

Please advise.

Thank you.

Regards,
Micheale
Go to Top of Page

micnie_2020
Posting Yak Master

232 Posts

Posted - 2013-07-25 : 06:03:24
Hi,

I had tested if same database different table name, it's work fine. But not different database and same tablename/different tablename.

Please advise.

Thank you.

Regards,
Micheale
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-25 : 06:49:42
quote:
Originally posted by micnie_2020

Hi,

I had tested if same database different table name, it's work fine. But not different database and same tablename/different tablename.

Please advise.

Thank you.

Regards,
Micheale


you need to do identity insert code from right database context.

try



Alter TRIGGER BrandTriggerINSERT ON tblBrand after INSERT AS

EXECUTE Praf.[dbo].[sp_executesql] N'SET IDENTITY_INSERT dbo.tblBrand ON'

insert into Praf.dbo.tblBrand
(BrandID,[Description],TypeID)
select BrandID,Description,TypeID from inserted

EXECUTE Praf.[dbo].[sp_executesql] N'SET IDENTITY_INSERT dbo.tblBrand OFF'

GO



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-07-25 : 14:22:47
Looks like you're trying to do your own home made transactional replication. Have you considered using MSSQL Server's version for that?

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -