Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to copy all data from one sql table to another sql table in vb.net. Both Sql tables are in the same database. Here is my Query -
VB
SqlCmd = New SqlCommand("INERT INTO table1 SELECT * FROM table2", SqlCon)

But its giving error that incorrect syntax near INTO.
Whats wrong in it?
Posted
Updated 15-Mar-15 21:51pm
v2
Comments
Member 12654087 23-Nov-16 15:38pm    
how to pass id of first table to second table?

Proper usage of INSERT statement[^] is:
SQL
INSERT INTO Table1 (<Field_list>)
SELECT <Field_list>
FROM Table2


Never use *!
Note: Data type of source and destination fields must be the same!


Another way is to use SqlBulkCopy class[^].
 
Share this answer
 
v2
Comments
[no name] 16-Mar-15 4:00am    
5ed. I should have mentioned that. I guess I was too occupied with that pun :-)
Maciej Los 16-Mar-15 4:01am    
Thank you ;)
You have to insert an S into your insert :-)

INSERT INTO table1 SELECT * FROM table2
 
Share this answer
 
Comments
Maciej Los 16-Mar-15 3:57am    
The error message does not indicate that the reason of error is bad sql statement, but i can be wrong ;) I think it's OP's typo. Please, see my answer.
[no name] 16-Mar-15 4:03am    
From my experience it does - nonetheless it's good practise to do it like you proposed.
Maciej Los 16-Mar-15 4:02am    
5ed!
OP marked your answer as a solution. So, you were right! Hawk eye ;)
[no name] 16-Mar-15 4:04am    
Thank you ;-)
Member 12654087 23-Nov-16 15:39pm    
how to pass id of first table to second table? in vb.net

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