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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 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 |