Solved

Append to a SQL table from an array

Posted on 2013-11-20
5
270 Views
Last Modified: 2013-11-20
Is there a way to INSERT INTO a table from an array?

I do not have permissions for a BULK INSERT.

The array has 2 columns.

Col1 needs to be inserted into Col1 of the table.

Col2 needs to be inserted into Col2 of the table.

Can the array can be passed as a parameter into the stored procedure?

-- @arrExceptions  This is the 2 column array

      @SymbolID int,   -- From col1,row i of the array
      @Curr5DayAvg float, -- From col2,row i of the array


INSERT INTO Exceptions FROM the array.

(SymbolID, Curr5DayAvg)

VALUES (@SymbolID,@Curr5DayAvg)
0
Comment
Question by:Dovberman
  • 3
5 Comments
 
LVL 25

Expert Comment

by:SStory
Comment Utility
I am not aware of the array being able to be passe from VB/VC# to the stored proc. However it is easy enough to write a
SQLCommand object with parameters such that you can loop

for (int i=0;i<whatever to of array is;i++) {
        'stuff ArrayName[i,0] into  into SQL command parameter variable
        'repead with ArrayName[i,1] into the second
         'Execute the SQL Command

         'repeat process
}

Here's an example:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx
0
 

Author Comment

by:Dovberman
Comment Utility
I am doing something like this now.

The array has 1500 rows. It requires the sql statement to be executed 1500 times.

This times out the server.

Does the proposed method require the sql statement to be executed 1500 times?

 'Execute the SQL Command

         'repeat process
0
 
LVL 2

Accepted Solution

by:
Subhasree earned 500 total points
Comment Utility
An array cannot be passed as an arguement to a stored procedure.

If executing the sql command 1500 times is not practical for you,
You can write this in to xml file. Later execute a stored proced which reads the xml and insert the rows to the table without using BULK INSERT.
0
 

Author Comment

by:Dovberman
Comment Utility
Thanks.  I will write the rows to an xml file.
0
 

Author Closing Comment

by:Dovberman
Comment Utility
Thanks,

I will use the xml file approach.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now