Solved

Append to a SQL table from an array

Posted on 2013-11-20
5
280 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
ID: 39662675
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
ID: 39662935
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
ID: 39663341
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
ID: 39663519
Thanks.  I will write the rows to an xml file.
0
 

Author Closing Comment

by:Dovberman
ID: 39663524
Thanks,

I will use the xml file approach.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

860 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