Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk insert in SQL Server

I have created SSIS package that imports data to my table from csv, I am trying to reproduce SSIS via sql command, more specifically, with bulk insert statement, could you help?

csv file properties:

enter image description here

enter image description here

like image 229
dmitry Avatar asked Jan 23 '14 10:01

dmitry


2 Answers

You can use Bulk Insert for the purpose:

BULK INSERT table_name
FROM 'c:\test.csv'
WITH
(
   FIRSTROW= 3,
   FIELDTERMINATOR = ';',
   ROWTERMINATOR = '\n'
)
like image 186
Kiril Rusev Avatar answered Sep 28 '22 14:09

Kiril Rusev


Followng is not using bulk insert command – but using SqlBulkCopy class in .Net for SSIS script task

Following is the script task for copying data from one table into it’s history database (in another server) using Bulk Copy approach inside SSIS Script task. It uses ADO Connection in SSIS.

Import Namespaces

Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Imports System.Math
Imports System.IO
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Windows.Forms

CODE

 Public Sub PerformBulkCopy(ByVal tableName As String, ByVal sourceConnection As SqlConnection, ByVal destinationConnection As SqlConnection)
    Dim dtSource As DataTable
    dtSource = New System.Data.DataTable()
    dtSource = GetSourceDataTable(tableName, sourceConnection)


    Using sbc As New SqlBulkCopy(destinationConnection)
        sbc.DestinationTableName = tableName
        sbc.WriteToServer(dtSource)
    End Using
End Sub


Public Function GetSourceDataTable(ByVal tableName As String, ByVal sourceConnection As SqlConnection) As DataTable

    Dim cmd As New SqlCommand("uspGetDataForAchival")
    cmd.Parameters.Add(New SqlParameter("@TableName", tableName))
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = sourceConnection

    Dim dap As New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    dap.Fill(ds)
    Return ds.Tables(0)

End Function

Public Sub Main()

    Dim tableName As String = Dts.Variables("User::CurrentTable").Value.ToString()
    Dim sourceConnection As SqlConnection
    Dim destinationConnection As SqlConnection

    Try
        ' Retrieve the reference to the managed connection'
        Dim cmSource As ConnectionManager
        cmSource = Dts.Connections("ADOSourceConnection")
        sourceConnection = DirectCast(cmSource.AcquireConnection(Dts.Transaction), System.Data.SqlClient.SqlConnection)

        Dim cmDestination As ConnectionManager
        cmDestination = Dts.Connections("ADOACIConnection")
        destinationConnection = DirectCast(cmDestination.AcquireConnection(Dts.Transaction), System.Data.SqlClient.SqlConnection)

        Dim destinationConnectionString As String = destinationConnection.ConnectionString
        MessageBox.Show(destinationConnectionString)
        PerformBulkCopy(tableName, sourceConnection, destinationConnection)

        'Release Connection'
        cmSource.ReleaseConnection(sourceConnection)
        cmDestination.ReleaseConnection(destinationConnection)

        'Success'
        Dts.TaskResult = Dts.Results.Success


    Catch ex As Exception

        Dim exceptionVariable As Microsoft.SqlServer.Dts.Runtime.Variables = Nothing
        Dts.VariableDispenser.LockOneForWrite("User::ScriptException", exceptionVariable)
        exceptionVariable("User::ScriptException").Value = ex.Message
        exceptionVariable.Unlock()
        Dts.Events.FireError(-1, "Task Name", ex.Message, String.Empty, 0)
        Dts.TaskResult = Dts.Results.Failure
     End Try

    End Sub
like image 41
LCJ Avatar answered Sep 28 '22 15:09

LCJ