Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / October 2006



Tip: Looking for answers? Try searching our database.

SELECT * INTO versus INSERT INTO

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darrell Wesley - 25 Oct 2006 16:57 GMT
Is there any significant reson why I should use one of these verions of code
over the other. Both are using JET 4.0 databases.

------------original code---------------------------------------
       sql = "DELETE * FROM QCOILS"
       Set cmd = New ADODB.Command
       cmd.CommandText = sql
       cmd.CommandType = adCmdText
       cmd.ActiveConnection = cnnWork1
       cmd.Execute
       sql = "INSERT INTO QCOILS SELECT * FROM " & MyCcid
       cmd.CommandText = sql
       cmd.CommandType = adCmdText
       cmd.ActiveConnection = cnnWork1
       cmd.Execute
       Set cmd = Nothing

-----------------modified code------------------------------------------------
           sql = "DROP TABLE QCOILS"
           With cmd
               .CommandType = adCmdText
               .CommandText = sql
               .ActiveConnection = cnnWork1
               .Execute
           End With
    sql = "SELECT * INTO QCOILS FROM " & MyCcid  
   Set cmd = New ADODB.Command
   With cmd
       .ActiveConnection = cnnWork1
       .CommandType = adCmdText
       .CommandText = sql
       .Execute
   End With
   Set cmd = Nothing
Dmitriy Antonov - 25 Oct 2006 18:08 GMT
> Is there any significant reson why I should use one of these verions of
> code
[quoted text clipped - 32 lines]
>    End With
>    Set cmd = Nothing

There is and very significant.
Select ... Into re-creates the table (means that existing data is
destroyed). You don't even need to have this table defined and if you do
have, you may have it with totally different structure - it doesn't matter
because, as I said, the table is created from scratch. On another hand,
Insert Into appends new data to existing records - the table must exist at
that time and it must have the structure, which matches Select clause.

Dmitriy.
Darrell Wesley - 25 Oct 2006 19:21 GMT
I'm aware of that but given the fact that the end result is the same is there
any benefit in using one versus the other? Are there any "gotchas" to worry
about? Is there a speed difference?

> > Is there any significant reson why I should use one of these verions of
> > code
[quoted text clipped - 42 lines]
>
> Dmitriy.
Dmitriy Antonov - 25 Oct 2006 19:42 GMT
> I'm aware of that but given the fact that the end result is the same is
> there
> any benefit in using one versus the other? Are there any "gotchas" to
> worry
> about? Is there a speed difference?

Since Select...Into must delete a table, if exists, and create a new one,
this should take some time, but it should be insignificant depending on
number of records inserted. Insert Into may reduce performance, if it has
indexes defined on the table. I never measured the difference (and don't
remember any information about it) so can't tell you for sure. I can guess
that if you have a lot of records to delete, then dropping the table can be
faster than deleting records. But remember, that if you have something
defined on that table (indexes, default values, nullability and other
things) they are not preserved by Select ... Into statement, AFAIK.

Dmitriy.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.