Import large Excel file into SqlServer
I have to import the list of NGO from Romania to SqlServer. Before seeing my solution , please do it yourself: the file could be found at http://www.just.ro/registrul-national-ong/ and the name is Asociatii
I have tried with SSMS – no luck ( text will be truncated, even when I put nvarchar(max))
The I tried with flat file from CSV – same
Finally, I have solved with a little bit of VBA : first I have tried to write the insert into file and execute with sqlcmd. The file was too large, so sqlcmd does not report any errors( and not insert anything)
So the solution was to generate a batch file that have this
sqlcmd -S . -E -d InfoRo -Q “INSERT INTO [dbo].[Asociatii$] ([Denumire] ,[Numar inreg Reg National],[pozitie inchisa (da/nu)],[Starea actuala],[Judet],[Localitate],[Adresa],[Asociati/Fondatori],[Scop],[Consiliu director],[Apartenenta federatie],[HG utilitate publica],[Data HG utilitate publica],[F14])values( ////)”
generated by this VBA script
Option Explicit Sub x() Dim startInsert As String startInsert = "sqlcmd -S . -E -d InfoRo -Q ""INSERT INTO [dbo].[Asociatii$]" startInsert = startInsert & " ([Denumire]" startInsert = startInsert & " ,[Numar inreg Reg National]" startInsert = startInsert & ",[pozitie inchisa (da/nu)]" startInsert = startInsert & ",[Starea actuala]" startInsert = startInsert & ",[Judet]" startInsert = startInsert & ",[Localitate]" startInsert = startInsert & ",[Adresa]" startInsert = startInsert & ",[Asociati/Fondatori]" startInsert = startInsert & ",[Scop]" startInsert = startInsert & ",[Consiliu director]" startInsert = startInsert & ",[Apartenenta federatie]" startInsert = startInsert & ",[HG utilitate publica]" startInsert = startInsert & ",[Data HG utilitate publica]" startInsert = startInsert & ",[F14])values(" Dim i As Long, j As Integer, text As String Dim insert As String For i = 2 To 86813 insert = insert & startInsert For j = Asc("A") To Asc("M") text = Sheet1.Range(Chr(j) & i).text text = Replace(text, vbCr, " ") text = Replace(text, vbLf, " ") text = Trim(text) text = Replace(text, "'", "''") text = Replace(text, " ", " ") insert = insert & "'" & text & "'," Next j insert = insert & "null)" & """" insert = insert & vbCrLf If (i Mod 150 = 0) Then Open "C:\Users\Surface1\Desktop\2Percent\a.bat" For Append As #1 Print #1, insert Close #1 insert = "" End If Next i If (Len(insert) > 0) Then Open "C:\Users\Surface1\Desktop\2Percent\a.bat" For Append As #1 Print #1, insert Close #1 insert = "" End If End Sub