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

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Bulk insert of Data from one table to another table

Posted By:      Posted Date: October 21, 2010    Points: 0   Category :Sql Server


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

View Complete Post

More Related Resource Links

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

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

sql insert very slow on bulk table



We are using SQL Server 2005 x86 enterprise edition. We have one table which has 13 crores / 130 million records. When we insert a record in that table it is taking around 30 seconds. Can u please help optimize it.

The table currently has 1 primary key (Clustered Index) and 4 non clustered index.

Please help.



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?

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'

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,

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-

SQL Update and Insert: Inserting a whole data table into another table



If I create a data tabe in C# like so:

datatable t = new DataTable;

DataColum C1 = new DataColum;//ID column


//the same with column C2

Then I fill up the table with some data.

Then the goal is to either insert this data or update data in an SQLtable in SQL. If the data is inserted, the ID column in t would be empty, if updated, it would not be. So in the insertion case, the sql query needs to make sure that unique IDs are assigned during insertion, in the update case, it just has to look for the right IDs.

How to go about this?

Asking because I am only familiar with updating and inserting single lines into a table. One could loop in C# over the datatable t of course and insert or update line by line, but this would probably be slow. Is there a way to write an SQL query like this somehow:

Insert Into SQLTable Values (@t)


And Update like this:

Update SQLTable SET ... Where C1 = @t.C1

Or something like that.

I know that SQL server can create automatic update and insert queries and that you can then type things in C# like


But, this has proven to be super slow on our server, for unknown reasons. I have asked on this forum and got some answers from experts, but was not able to srot out the problem.

How can we write

How to insert dynamic data into an XML or HTML format table


Hi there,

I have a technical problem as the title described. I need to achieve this by SSIS that means the destination file could be XML or HTML. However, before inserting dynamic data from source, an XML or HTML format table must be predefined. In the final destination file, the predefined table and dynamic data will be both output.

Can anybody give some suggestions or reference links regarding to this? Appreciate it.


insert data from old table to new table


hi everyone

I meet a question.I need insert data from A table to B table.they are same structure,just data time different.A table content from 1/1/2010 to 10/1/2010,B table content from 9/1/2010 to now. (note I  used SSIS bulk insert september month data from A to B )

now I need insert three month 8,9,10 from A table to B table.when I insert data ,I meet this error  

Violation primary key condition constraint , cannot insert duplicate index.becasue B table have
september data.
not use insert B from A where (WHERE DATA_TIME BETWEEN CAST('2010/08/01 00:00:00' AS DATETIME) AND CAST('2010/09/30 23:59:59' AS DATETIME)
who can help me resoveing this issue,thanks a lot!!!

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