.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

trigger on insert is failing for data insert in a table

Posted By:      Posted Date: November 02, 2010    Points: 0   Category :Sql Server

hello experts,

here is some question for you and i'm sure you guys will have awsome solution

i have a table with companyid and tranid as a primary key. there is a insert trigger on it and the trigger insert property is instead of insert, means data is manually inserted thru trigger.

in trigger, i check if companyid and tran id exists in the table or not, if yes then i add a record in another table (history table/duplicate table) and if not found then i add record in this table.

this stuff work perfectly if i work in ssms and it is all good but when i work thru ssis, it gives me following error and it clearly showing that it is because of pk

[Insert Data in EFT [46]] Error: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "The statement has been terminated.".
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "Violation of PRIMARY KEY constraint 'PK_Mail_Queue'. Cannot insert duplicate key in object 'dbo.EMail_Queue'.".

any solution

mark it as answer if it answered your question :)

View Complete Post

More Related Resource Links

In a trigger can up insert data into a table located on a linked server?


EXEC master.dbo.sp_addlinkedserver @server = N'ServerB', @srvproduct=N'SQL Server'
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'ServerB',@useself=N'False',@locallogin=NULL,@rmtuser=N'lnklogin',@rmtpassword='########'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'collation compatible', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'data access', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'dist', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'pub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'rpc', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'rpc out', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'sub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'connect timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'collation name', @optvalue=null
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'lazy schema validation', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ServerB', @optname=N'query timeout', @optvalue=N'0'

reading xml data and insert into database table

hi friends, .very urgent for me so only i send following script i create for read xml data and to import to table. i got result for reading xml and rows are added to table but the values are showing null pls clarify this problem. thanks alter procedure pizza_sales ( @xml xml ) as begin INSERT INTO pizza_xml (LOC_CODE, CUST_CODE, PRODUCT_ID, INV_TYPE, INV_NO, INV_DT, INV_QTY_IN_INV_UOM, INV_RATE_IN_BS_CURR, INV_VU_IN_INV_CURR, -- TOTAL_DISC_IN_BS_CURR, NET_INV_VU_IN_INV_CURR, NET_INV_VU_IN_BS_CURR, TOTAL_TXES_IN_BS_CURR ) select table1.column1.value('@LOC_CODE','nvarchar(75)'), table1.column1.value('@CUST_CODE','nvarchar(75)'), table1.column1.value('@PROD_CODE','nvarchar(75)'), table1.column1.value('@INV_TYPE','nvarchar(50)'), table1.column1.value('@INV_NO','nvarchar(100)'), table1.column1.value('@INV_DT','datetime'), table1.column1.value('@INV_QTY_IN_INV_UOM','numeric(28,8)'), table1.column1.value('@INV_RATE_IN_BS_CURR','numeric(28,8)'), table1.column1.value('@INV_VU_IN_INV_CURR','numeric(28,8)'), --table1.column1.value('@TOTAL_DISC_IN_BS_CURR','numeric(28,8)'), table1.column1.value('@NET_INV_VU_IN_INV_CURR','numeric(28,8)'), table1.column1.value('@NET_INV_VU_IN_BS_CURR','numeric(28,8)'), table1.column1.value('@TOTAL_TXES_IN_BS_CURR','numeric(28,8)') from @xml.nodes('pizza/pizzaxml')as table1(column1) end declare @id

pull data from table A and insert into the same table

Hi All, When I pull data from one table and try to insert into the same table, is it possible for any erroneous data loading? ex:   Insert Into TableA Select Cat,Bank, City, Act From TableA A CROSS APPLY (SELECT COUNT(ACT) AS TOT from TableA ) B   Thanks        shamen

How to insert values into my table the data is coming from inline table valued function

Hi all,   i have table oi want insert values into my table the data is coming from inline table valued function how i can use the inline table valued function to insert datainto my table i have inline function which out puts 3 collumns of data how i can insert this data into my table i have the insert statment like below insert into tablename(c1,c2,c3,c4,c5) select ('abc',....................,'xyz') how i can use the out put put of my inline function to  to insert data into my columns c2,c3,c4. please help me out Thanks & Regards Sunil Yoganna

automatic insert data to mysql table

hi all,             Actually i need to transfer data from one table to another table in mysql with codition of Time.for example i need to transfer data at 10.00am when user login at 10.00am it must be insert into another data table.. if anybody know time control  please reply me..C#Thank You,Arunabathan.G

After insert trigger on the same table.

Hi! i have the following table. CREATE TABLE [dbo].[EICANALESRES]( [IdContrato] [decimal](10, 0) NOT NULL, [EiCanalId] [decimal](10, 0) NOT NULL, [EiFechServ] [datetime] NOT NULL, [EiFechMaq] [datetime] NOT NULL, [EiFechCorr] [datetime] NOT NULL, [EiValorFis] [decimal](25, 4) NOT NULL, [EiValorNum] [decimal](25, 4) NOT NULL, [EiValorCalc] [decimal](25, 4) NOT NULL, [EiEst] [smallint] NOT NULL, PRIMARY KEY CLUSTERED ( [IdContrato] ASC, [EiCanalId] ASC, [EiFechServ] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]  I want a new record that takes all the data from from the recently inserted record where idcontrato =2 and eicanalid =91. And then give this new record a new idcontrato. Without deleting the original record. i have this so far, but it erases the original record.   alter TRIGGER [dbo].[datosdemo] ON [dbo].[EICANALESRES] AFTER INSERT AS BEGIN SET NOCOUNT ON; delete from eicanalesres where idcontrato = '13' and eicanalid = '91' and eivalorfis = '0' update dbo.eicanalesres SET idcontrato = '13' FROM INSERTED WHERE eicanalesres.idcontrato = inserted.idcontrato AND eicanalesres.eicanalid = inserted.eicanalid and eicanalesres.eifechserv = inserted.eifechserv and inserted.idcontrato = '02' and inserted.eicanalid = '91'

how can insert only new record into another table through trigger

I have on master table in sql in which every time new data is inserted.i just want to copy the new inserted record into another table.How can i do this. below is my trigger but how can it identify only new records should be inserted into second table.please help me and modify it. CREATE TRIGGER CopyRecordToPortInTable ON SmsTable AFTER INSERT AS BEGIN SET NOCOUNT ON; insert into Portintable(smsno,smstext,flag) select (sms,smstext,0) where condition END

How can we insert csv file data into Sql server table based on some condition ?


Hello All experts there,


I need to insert some csv file data into sql server 2008. but there are multiple tables created in sql server and I need to check some conditions before data insertion and based on the condition value, sql script should insert the data in to specific table. in other words I need to insert data from one csv file to different tables based on some condition.

is it possible? if yes then can anyone please give me an idea on how can we do that?


Thanks in advance


Nipendra Garg

how to insert data present in different rows of same collumn of one table into same rows of differn



I am have task that i have to insert data present in different rows of same collumn of one table into  same rows of differnt collumn of other table below shows the table structure source & distination

c_date                c_balance

11/1/2010          100

11/2/2010          200

11/3/2010          300

     ----                ----

  -----                ---

12/1/2010          150

12/2/2010          300

12/3/2010          400

   ---                  ----

   ----                  ---

like above i have thousands of data with other collumns

now i have the detination structure  as

Insert data from SqlServer Table to ExcelSheet

It works fine. Because information is put on Excel cells how is hoped. However, at least, with Office 2007 always ask when the file is opened if you are sure because the format of file has a diferent format from the extension.

Here the Example:

Insert data in parent and child table at the same time


I have two (parent-child) tables that are bound by a primary key (PK)/foreign key (FK) relation.each PK is identity (idStudent and idBac)

to insert data, i'm using an little application in c# i programmed myself where i insert these columns for the Student table: F_Name, L_Name, Age, Mobile, Adresse, City ,and  ( i don't know how to handle the idBac wich is foreign key of the other table wich still not inserted yet)

for the table Bac, i insert section, school, and city.

what i want is when i click on "OK" button, the idBac ,wich is a foreign key of the other table (BAC), receives the same idBac given the moment i inserted the BAC informations too ...

i don't have any problem wich ado.net ...i just want the right query/procedure.

thanks in advance and sorry for my english, it's not my mother tongue.

How to using XML to insert data to table



I have a stored procedure that receives a XML type. I want to take the XML data and insert the records to a table. what is the better way to insert the records?


Is there a way the I do not need to use fetch or any loop? Could I use some like Insert (...) from XML?

Insert XML data into table problem


For the life of me I cannot figure out what's wrong with my SP. I'm receiving an XML document with a bunch of invoices like


And I need to write each invoice as a record in SQL Server 2005

I'm getting the following error

Msg 170, Level 15, State 1, Procedure pcdAddInvoices, Line 39
Line 39: Incorrect syntax near '.'.


 -- Add the parameters for the stored procedure here

 @msg xml
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.

    -- Insert statements for procedure here
 INSERT INTO [Promacoin].[dbo].[Tblinvoices_DAX] (ObjectID,ActivityID,ResourceID,ContractID,Document
 SELECT M.Invoice.value('/ObjectID','NVARCHAR(16)') AS ObjectID,

SQL Trigger INSERT, UPDATE table name (inserted updated)

I am trying to make a trigger for INSERT, UPDATE.  For INSERT you get a table named inserted that you can use, and for UPDATE you get updated.  But what is the name if you doing a trigger for both?

Insert Data to table from excel file


guys, i am new to sql development

my question is how do i import data from an excel table,

i was trying to do it via a query and just paste the values, but received a syntax error. thanks

data structure is:


How to INSERT GridView data into another table (besides the one it is bound to)??


Hi All,

I have a tricky situation that I just cannot seem to figure out.  I'm using ASP.NET 4.0.  I have two databases (let's call them Northwind and MyDatabase for simplicity).  I cannot modify the data in Northwind, but once I save a copy to MyDatabase, I can change it.

I have a GridView that shows all the tests associated with a patient.  I first populate the GridView, retrieving all the tests in Northwind.Test that are associated to a PatientID.  On a button click event, I need to:

  1. Create a new Patient record in MyDatabase.Patient (I've got this part done already).
  2. Use that newly created PatientID and INSERT all of the rows from the GridView into MyDatabase.Test.  I have the PatientID available, I just don't know how to retrieve the data from the GridView.
  3. (Ideally) I would like to then be able to load that patient record in the future from MyDatabase and be able to UPDATE the tests in the GridView to MyDatabase.Test.

I can load the tests into the GridView no problem.  But I have no idea how I would save that data to a different datasource.  Any ideas?  Should I even be using a GridView, or something else?

-Thanks in advance-

Bulk insert of Data from one table to another table



I have one table (TABLE A) which updates with milions of records every day. Now Every day we clear the data of this TABLE A; but before clear the data from TABLE A we get all data from TABLE A and move it into another table TABLE B due to some purpose and we are using 

INSERT INTO TABLE B (col1,col2,.......colN) values (SELECT col1,col2,..........colN from TABLE A)

This approch is not giving us a best performance as we are selecting all data from TABLE A and then inserting into TABLE B in one shot and i have millions of rows in my TABLE A (sometimes 16 millions).

Can anyone suggest any other approch to do so that i can get best performance ?


JP Sharma
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend