Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we insert into a normal table from temp tables in sql 2008?

I have written query as
insert into Table1 Values (Field1, Field2) Select Fld1, Fld2 From ##TmpTable
Posted
Comments
ZurdoDev 5-Jul-12 8:59am    
So, what's the problem?

I think you have a small error on your syntax. It should be more like this:

INSERT INTO table1
(Field1, Field2)
SELECT Fld1, Fld2
FROM ##TmpTable
 
Share this answer
 
You can use SqlBulkCopy method, here is the C# code

C#
using (SqlConnection connSource = new SqlConnection(connectionstring))
using (SqlCommand cmd = connSource.CreateCommand())
using (SqlBulkCopy bcp = new SqlBulkCopy(connectionstring))
{
    bcp.DestinationTableName = "table1";
    cmd.CommandText = "SELECT Fld1, Fld2 FROM ##TmpTable";
    cmd.CommandType = CommandType.Text;
    connSource.Open();
    using (SqlDataReader reader = cmd.ExecuteReader())
    {
        bcp.WriteToServer(reader);
    }
}


VB.net converted code, i'm not VB.net developer, please get only the idea and proceed.

VB
Using connSource As New SqlConnection(connectionstring)
	Using cmd As SqlCommand = connSource.CreateCommand()
		Using bcp As New SqlBulkCopy(connectionstring)
			bcp.DestinationTableName = "table1"
			cmd.CommandText = "SELECT Fld1, Fld2 FROM ##TmpTable"
			cmd.CommandType = CommandType.Text
			connSource.Open()
			Using reader As SqlDataReader = cmd.ExecuteReader()
				bcp.WriteToServer(reader)
			End Using
		End Using
	End Using
End Using
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900