Import CSV file into SQL Server

后端 未结 12 1418
梦谈多话
梦谈多话 2020-11-22 15:41

I am looking for help to import a .csv file into SQL Server using BULK INSERT and I have few basic questions.

Issues:

相关标签:
12条回答
  • 2020-11-22 16:30

    Here's how I would solve it:

    1. Just Save your CSV File as a XLS Sheet in excel(By Doing so, you wouldn't have to worry about delimitiers. Excel's spreadsheet format will be read as a table and imported directly into a SQL Table)

    2. Import the File Using SSIS

    3. Write a Custom Script in the import manager to omit/modify the data you're looking for.(Or run a master script to scrutinize the data you're looking to remove)

    Good Luck.

    0 讨论(0)
  • 2020-11-22 16:35

    You first need to create a table in your database in which you will be importing the CSV file. After the table is created, follow the steps below.

    • Log into your database using SQL Server Management Studio

    • Right click on your database and select Tasks -> Import Data...

    • Click the Next > button

    • For the Data Source, select Flat File Source. Then use the Browse button to select the CSV file. Spend some time configuring how you want the data to be imported before clicking on the Next > button.

    • For the Destination, select the correct database provider (e.g. for SQL Server 2012, you can use SQL Server Native Client 11.0). Enter the Server name. Check the Use SQL Server Authentication radio button. Enter the User name, Password, and Database before clicking on the Next > button.

    • On the Select Source Tables and Views window, you can Edit Mappings before clicking on the Next > button.

    • Check the Run immediately check box and click on the Next > button.

    • Click on the Finish button to run the package.

    The above was found on this website (I have used it and tested):

    0 讨论(0)
  • 2020-11-22 16:36

    The best, quickest and easiest way to resolve the comma in data issue is to use Excel to save a comma separated file after having set Windows' list separator setting to something other than a comma (such as a pipe). This will then generate a pipe (or whatever) separated file for you that you can then import. This is described here.

    0 讨论(0)
  • 2020-11-22 16:36

    Because they do not use the SQL import wizard, the steps would be as follows:

    enter image description here

    1. Right click on the database in the option tasks to import data,

    2. Once the wizard is open, we select the type of data to be implied. In this case it would be the

    Flat file source

    We select the CSV file, you can configure the data type of the tables in the CSV, but it is best to bring it from the CSV.

    1. Click Next and select in the last option that is

    SQL client

    Depending on our type of authentication we select it, once this is done, a very important option comes.

    1. We can define the id of the table in the CSV (it is recommended that the columns of the CSV should be called the same as the fields in the table). In the option Edit Mappings we can see the preview of each table with the column of the spreadsheet, if we want the wizard to insert the id by default we leave the option unchecked.

    Enable id insert

    (usually not starting from 1), instead if we have a column with the id in the CSV we select the enable id insert, the next step is to end the wizard, we can review the changes here.

    On the other hand, in the following window may come alerts, or warnings the ideal is to ignore this, only if they leave error is necessary to pay attention.

    This link has images.

    0 讨论(0)
  • 2020-11-22 16:37

    I know that there are accepted answer but still, I want to share my scenario that maybe help someone to solve their problem TOOLS

    • ASP.NET
    • EF CODE-FIRST APPROACH
    • SSMS
    • EXCEL

    SCENARIO i was loading the dataset which's in CSV format which was later to be shown on the View i tried to use the bulk load but I's unable to load as BULK LOAD was using

    FIELDTERMINATOR = ','
    

    and Excel cell was also using , however, I also couldn't use Flat file source directly because I was using Code-First Approach and doing that only made model in SSMS DB, not in the model from which I had to use the properties later.

    SOLUTION

    1. I used flat-file source and made DB table from CSV file (Right click DB in SSMS -> Import Flat FIle -> select CSV path and do all the settings as directed)
    2. Made Model Class in Visual Studio (You MUST KEEP all the datatypes and names same as that of CSV file loaded in sql)
    3. use Add-Migration in NuGet package console
    4. Update DB
    0 讨论(0)
  • 2020-11-22 16:39

    Import the file into Excel by first opening excel, then going to DATA, import from TXT File, choose the csv extension which will preserve 0 prefixed values, and save that column as TEXT because excel will drop the leading 0 otherwise (DO NOT double click to open with Excel if you have numeric data in a field starting with a 0 [zero]). Then just save out as a Tab Delimited Text file. When you are importing into excel you get an option to save as GENERAL, TEXT, etc.. choose TEXT so that quotes in the middle of a string in a field like YourCompany,LLC are preserved also...

    BULK INSERT dbo.YourTableName
    FROM 'C:\Users\Steve\Downloads\yourfiletoIMPORT.txt'
    WITH (
    FirstRow = 2, (if skipping a header row)
    FIELDTERMINATOR = '\t',
    ROWTERMINATOR   = '\n'
    )
    

    I wish I could use the FORMAT and Fieldquote functionality but that does not appear to be supported in my version of SSMS

    0 讨论(0)
提交回复
热议问题