?
Solved

INSERT array at once into SQL server ??

Posted on 2008-10-24
16
Medium Priority
?
2,507 Views
Last Modified: 2008-11-05
Hello,

I have the attached code. Is there a way to put the array into the table at once without looping through it?
The attached code takes much too long to process.

Regards,
Dennis
Function Add_Record1()
 
'Dim cn As ADODB.Connection
Dim rs As Recordset
'Set cn = New ADODB.Connection
 Dim name
  name = Text3.Text
 Dim query
 
 
For y = 1 To numberofsignals
For i = 1 To numberofrows
If i - Tradingdelay >= 1 Then
If close_price(i) <> "" And signal(i - Tradingdelay, y) <> "" And close_price(i - Tradingdelay) <> "" Then
 
query = "INSERT INTO " & name & ".dbo.[Data] ([Date],Signal,Equity) values ('" & datum(i) & "','" & signal(i, y) & "','" & equity(i, y) & "')"
Set rs = ado.Execute(query)
End If
End If
Next
Next
  
Debug.Print ("Processingnr:" & processingnumber)
 
    
End Function

Open in new window

0
Comment
Question by:Schuttend
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22798873
you can put all your rows you like to enter into a single SQL-query, like

INSERT INTO dbo.[Data] ([Date],Signal,Equity) values ('Date1','Signal1,'Equity1'), ('Date2','Signal2,'Equity2'), ('Date3','Signal3,'Equity3')
0
 

Author Comment

by:Schuttend
ID: 22798885
Hi,

I have more then 100.000 values, this is just not possible....
0
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22798939
is serializing the data to a csv file and using bulk insert an option?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Schuttend
ID: 22798957
I have never heard about this, but maybe it is. Can you tell me more about this?
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22798997
Well, you have to either insert with a single query or insert one at a time.

However, the thing that might be slowing you down is updating database indexes.  With each insert call, the database has to update all indexes for the table for which you are inserting records.  Record inserts go much faster on a table without indexes.  

A few ideas:
1. Drop the indexes from the table you are updating, insert all the records, then reapply the indexes (bad idea if you already have way more records in the database compared to the number of records being added).
2. Create a duplicate table in the database, but don't include any indexes.  Insert all of the records into the duplicate table.  Then with a single query, copy the records from the duplicate table to the main table.
3. If this is a new database being constructed on the fly, add indexes AFTER all the records have been inserted into the table.
0
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22798996
you find a simple example here: http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/

basicly you send a file with all the rows to sql server and tell it how the data is structured.

You find more detailed information here: http://msdn.microsoft.com/en-us/library/ms188365.aspx
0
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22799038
@HooKooDooKu:
you're right. Indexes might slow it down too. But i think the single 100'000 SQL-queries are the real bottleneck here.
0
 

Author Comment

by:Schuttend
ID: 22799059
Hi,

Thank for you that. But I need it to be programmed from within visual basic 6. Sorry for the confusion...
0
 
LVL 2

Expert Comment

by:Clausewitz
ID: 22800340
i don't know much about VB6, but i don't think inserting multiple rows will be possible with it.
0
 
LVL 22

Expert Comment

by:dportas
ID: 22802282
What is the source for the data? Depending on the source their may be different solutions. For example if it's a file you could use BCP or SSIS. If it's a query then you could insert it without having to loop through each row.
0
 

Author Comment

by:Schuttend
ID: 22804033
The source is an array of data, but I can make it a file as well.
Could you please give me an example how to insert a array at once? It's indeed a query....
0
 
LVL 22

Expert Comment

by:dportas
ID: 22804243
0
 

Author Comment

by:Schuttend
ID: 22806630
Is there any way to do this from within vb6??

Regards,
Dennis
0
 
LVL 22

Expert Comment

by:dportas
ID: 22806645
Yes. You can execute any of those T-SQL statements (BULK INSERT or OPENROWSET) from VB6 using ADO. ADO is the standard way to interface with SQL Server.

However, VB6 is effectively obsolete and has been de-supported for years. It normally shouldn't be used for new projects. Certain features won't be supported. For example in SQL Server 2008 you have the option of passing arrays of values using table-valued parameters. I don't think you'll be able to do that in VB6, although I haven't tried it myself from VB6 so I can't be absolutely sure.
0
 

Author Comment

by:Schuttend
ID: 22806655
Could you please give me an example of BULK INSERt in vb6 code? I am working with ADO in my current project, so this should work I guess.
0
 
LVL 22

Accepted Solution

by:
dportas earned 750 total points
ID: 22806670
The process is the same as for any other SQL command executed from ADO: Create a connection. Create the command object. Set the command text and call the Execute method. I can't find an example right now but if you consult the Help file or Google for ADO VB6 you'll find plenty of examples.

It's good practice to do all data access in SQL Server using stored procedures. So consider creating a SQL stored procedure to do the actual INSERT and then calling that from your VB code.

To use BULK INSERT the source file must be accessible to the server, must be specified using a physical drive letter or UNC path (not mapped drives) and the SQL service account must have permission to read that path and file.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question