Import DATA to SQL Server from CSV file - sql-server

I have a CSV file with 10 columns. I want to import to SQL Server table with only 5 columns using powershell. Can anyone help in this ?

I suggest that Change your excel file to 5 sight column, and then using SQL Server Import\Export wizard or SSIS (if your convert is each day periodic) or PowerShell to convert your data.
You can you following reference in order to use PowerShell to convert your file:
SQL SERVER – Powershell – Importing CSV File Into Database

Related

Error when import flat file into SQL Server

I'm trying to import a flat file .csv into SQL Server using SQL Server Management Studio. My process is:
Right-click the Database
Choose Task > Import Flat File
The .csv file contains more than 1100 columns with the size of 36KB . However, SQL Server warns
Sum of the columns' FillWeight values cannot exceed 65535. (System.Windows.Forms)
How can I solve this problem? Thanks!

Cannot import long text from Excel to SQL Server using SSIS

Environment:
Microsoft® Excel® for Microsoft 365 MSO (Version 2112 Build 16.0.14729.20254) 64-bit
Microsoft SQL Server 2019 (RTM-CU14) (KB5007182) - 15.0.4188.2 (X64)
Microsoft Visual Studio 2019
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\Office\16.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows set to 0
In Excel I formated the cell as "Text" and I also filled up the 2nd and 3rd rows with some super-long dummy text... (1st row is title row)
When I go to Excel Source/Advanced editor, I can set the Output to Unicode text stream [DT_NTEXT] or anything else but the External column cannot be changed to something else than Unicode string [DT_WSTR] (255 characters) despite the setting in registry that normally should allow it and despite having super-long strings in first two rows (other than the 1st row which holds the field names)
Then of course when I try to execute the SSIS task it throws a normal truncation error.
Question: What I am doing wrong or what else should be done here to actually be able to import the data? By the way, this is supposed to be automated at some point.
Since an Excel workbook is not a database, the OLE DB provider tries to detect the most relevant metadata from the Excel worksheet and read it as tabular data, which is mostly inaccurate when handling medium and large Excel files. After spending years on creating SSIS packages, I will convert the Excel file to a CSV file and import it using a Flat File Connection Manager instead. Or I will use a C# script to import the data.
I. Converting Excel to CSV
You can automate the process of converting Excel to CSV using a C# script:
Converting XLSX file using to a CSV file
Convert .xlsx & .xls to .csv
How to Convert Excel to CSV using Interop
After converting the Excel file to a CSV file, you can dynamically import it using a Flat File Connection Manager:
Dynamic Flat File Connections in SQL Server Integration Services
II. Using a C# script
It is good to check the following class, which is a part of the SchemaMapper project:
SchemaMapper - MsExcelImport.cs
Besides, a step-by-step guide on how to use this library can be found in the following link:
Import data from multiple files into one SQL table step by step guide
III. Editing the Excel connection string
If you don't have the choice to convert Excel to flat files, then you can force the Excel connection manager to ignore headers from the first row by adding IMEX=1 to tell the OLEDB provider to specify data types from the first row (which is the header - all string most of the time).
To edit the connectionstring property, click on the Excel Connection Manager and press on the F4 key. In the Properties Tab, you can edit the connectionstring property.
SSIS Excel Import Columns with More or Less than 255 Characters
IV. Changing columns length from advanced editor
Try changing the Excel Source column metadata from the advanced editor:
In SSIS excel datasource not taking more characters than 255
Importing Excel using SSIS may cause a headache! You can check the following question:
Workaround for exporting data to Excel with more than 255 columns
Dynamically Creating Excel table through SSIS
SQL Server Import Wizard doesn't support importing from excel sheet with more than 255 columns
Importing Excel Data Seems to Randomly Give Null Values
Failing to read String value from an excel column
Importing Excel Data Seems to Randomly Give Null Values
SSIS - Excel data shows as scientific notations and Null Values

SQL SERVER - reading excel files content and transfer to sql database using xp_cmdshell

I was shocked when I learned that importing the excel data to sql database using OPENROWSET has downsides as it truncates the cells' values of it to 255-length-characters before it passes to the database. I'm now thinking of using xp_cmdshell to read the excel file's data and transfer it to database. however I'm clueless on how I could do that. could somebody help me to achieve that?
Yes BCP could be used to import data from excel(.xlsx) files into Sql Server tables. Only thing to remember here is from MS documentation -
Prerequisite - Save Excel data as text To use the rest of the methods
described on this page - the BULK INSERT statement, the BCP tool, or
Azure Data Factory - first you have to export your Excel data to a
text file.
In Excel, select File | Save As and then select Text (Tab delimited)
(.txt) or CSV (Comma delimited) (.csv) as the destination file type.
A sample BCP command to import data from a excel file (converted to tab delimited) into Sql Server table -
bcp.exe "MyDB_Copy.dbo.Product" in "C:\Users\abhishek\Documents\BCPSample.txt" -c -t"\t" -r"\n" -S ABHISHEK-HP -T -h TABLOCK
Read more about BCP and import here.

Workaround for exporting data to Excel with more than 255 columns

SSMS and SSRS to Excel enable for more than 255 columns when copy-pasting.
SSIS does not allow for more than 255 columns to be exported to Excel 2007.
Is there a way to override this?
Problem
There are a lot of Limitations when exporting to an Excel Files using Sql server data tools
Workarounds
You can do some workaround to achieve this:
Create a dataflowtask that export your data into a FlatFile (csv)
Store your Destination FileName in a Variable
Create another Dataflowtask that convert your csv file to an Excel File using a script task with a similar Function
Note: you have to add Microsoft.Office.Interop.Excel.dll file to the following directories (.Net Framework dll directory) C:\Windows\Microsoft.NET\Framework\v2.0.50727 and (sql server data tools dll directory) C:\Program Files\Microsoft SQL Server\100\DTS\Binn (using vs 2005 and sql 2008) and then add this dll as a reference in your script task
Imports Microsoft.Office.Interop
Public Sub ConvertCSVToExcel(Fromcsv As String, Toxlsx As String)
Dim Exl As New Excel.Application()
Try
Dim wb1 As Excel.Workbook = Exl.Workbooks.Open(Fromcsv, Format:=4)
wb1.SaveAs(Toxlsx, FileFormat:=XlFileFormat.xlOpenXMLWorkbook)
wb1.Close()
Exl.Quit()
Catch ex As Exception
Exl.DisplayAlerts = False
Exl.Quit()
End Try
End Sub
Third party components
Or you have to use a third party components like cozyRoc SSIS+
Side Note
if you are looking to Import data from excel with more than 255 columns you can follow this Link
References
Third party components
SSIS: Export more than 255 columns from SQL table to Exce
Cozyroc website
Workaround
convert csv to xlsx
trying to use custom assembly with script task in SSIS 2008 - can't find correct version of GACUtil
Refer the link. Best would be to create an script in SSIS to copy the content as csv format. You can use c# or VB.Net.
just for anyone come to this page and use SQL Server Import and Export Wizard to export to excel
in tab Review Data Type Mapping
section Data Type Mapping: ==> double click your column that has length more than 255 char open its Column Conversion Details Dialog then close it
then next to end...Lool

How do I copy a csv file into a mssql database?

I am looking for help to migrate my mysql over to mssql.
I have taken the database from MySQL and created a .csv file.
But from here I get stuck.
I hope you can help me to understand this.
Using Sql Server Import Wizard
1) Open Sql Server Managment Studio
2) Go Import Wizard.
3) Point the Wizard at your file (I would recommend saving the csv to excel tho).
4) Map Columns to appropriate datatypes and lengths.
Open Sql Server Management Studio; connect to your server.
Right click your database in Object Explorer, and go to Tasks -> Import Data...
In the wizard that pops up, choose a Data Source of "Flat File Source", and browse to your csv file for File Name.
For Format, choose "Delimited".
For Text Qualifier, type in one double-quote character: ". This is an important step that will let SQL read in strings properly.
Depending on your file, check or un-check the "Column names in the first data row"
For the most part, this will import your file in. You may need to tweak some of the settings under the Advanced section if your data has text > 50 characters or numeric characters, but the Preview section will show you how the file looks so far.

Resources