SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to trigger after insert to new database tbl
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

micnie_2020
Posting Yak Master

Malaysia
226 Posts

Posted - 07/25/2013 :  02:51:53  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 07/25/2013 :  04:02:32  Show Profile  Reply with Quote
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

Malaysia
226 Posts

Posted - 07/25/2013 :  04:11:07  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 07/25/2013 :  04:32:09  Show Profile  Reply with Quote
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

Malaysia
226 Posts

Posted - 07/25/2013 :  05:14:48  Show Profile  Reply with Quote
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

Malaysia
226 Posts

Posted - 07/25/2013 :  06:03:24  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 07/25/2013 :  06:49:42  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 07/25/2013 :  14:22:47  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000