• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3048
  • Last Modified:

INSERT array at once into SQL server ??

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
Schuttend
Asked:
Schuttend
  • 6
  • 5
  • 4
  • +1
1 Solution
 
ClausewitzCommented:
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
 
SchuttendAuthor Commented:
Hi,

I have more then 100.000 values, this is just not possible....
0
 
ClausewitzCommented:
is serializing the data to a csv file and using bulk insert an option?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SchuttendAuthor Commented:
I have never heard about this, but maybe it is. Can you tell me more about this?
0
 
HooKooDooKuCommented:
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
 
ClausewitzCommented:
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
 
ClausewitzCommented:
@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
 
SchuttendAuthor Commented:
Hi,

Thank for you that. But I need it to be programmed from within visual basic 6. Sorry for the confusion...
0
 
ClausewitzCommented:
i don't know much about VB6, but i don't think inserting multiple rows will be possible with it.
0
 
dportasCommented:
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
 
SchuttendAuthor Commented:
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
 
SchuttendAuthor Commented:
Is there any way to do this from within vb6??

Regards,
Dennis
0
 
dportasCommented:
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
 
SchuttendAuthor Commented:
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
 
dportasCommented:
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

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!

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now