95 How to import a csv file into SQL Server using bulk insert SQL query

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ก.ย. 2024

ความคิดเห็น • 56

  • @BoneAppleTea
    @BoneAppleTea ปีที่แล้ว +2

    Great tutorial, opening it in a textpad is something other tutorials skip. 10/10

    • @learnssis
      @learnssis  ปีที่แล้ว

      Thank you Bone Apple Tea.

  • @9844468928
    @9844468928 2 ปีที่แล้ว +1

    in an interview they have asked this question and i have struggled a lot to write this simple query. Thank you so much for you help on this Bro

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      No problem. You are most welcome Keerthi.

    • @user-fb1ps9bv9g
      @user-fb1ps9bv9g ปีที่แล้ว

      Thank you for sharing this, it a tip now

  • @surajsonawane4270
    @surajsonawane4270 ปีที่แล้ว +1

    getting an error - Msg 102, Level 15, State 1, Line 9
    Incorrect syntax near 'format'.

  • @pratikshashukla6522
    @pratikshashukla6522 4 หลายเดือนก่อน +1

    How to insert a csv file if it contains multiple empty rows ( each column of that row is empty). I want either Bulk insert move to another row if it encounter empty row or it should insert that row with null values.

    • @learnssis
      @learnssis  3 หลายเดือนก่อน

      I think you can handle this using data flow task, that if a column value is empty then it can convert the value to NULL.
      th-cam.com/video/k7qqrtSY09o/w-d-xo.html

  • @peterglover7178
    @peterglover7178 2 ปีที่แล้ว

    Great, simple explanation - thanks for this.

    • @learnssis
      @learnssis  2 ปีที่แล้ว +1

      Thank you Peter.

  • @ameet6384
    @ameet6384 2 ปีที่แล้ว

    Awesome worked great!!!
    I was getting some errors while trying to import flat file feature due to large number of records (> 500k).
    The only thing I had to modify here was the file path location. Since my sql server instance was present on a remote sever, I had to use a shared folder path.

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      Nice to hear Ameet that it worked for you.

  • @priyankabachhav5315
    @priyankabachhav5315 ปีที่แล้ว

    Hello sir i want to perform bulk insertion but i am using SQLBindParameter and then SQLBulkOperations for executing but it's giving me error.. I tried finding you on linkdin but not found.. Will you please help me out?

    • @learnssis
      @learnssis  ปีที่แล้ว

      Hi Priyanka, I don't have much experience working with SQLBindParameter and then SQLBulkOperations, 99% of the time I use SSIS to import data from CSV\flat files to sql server table. If you are getting an error then try to google it or you can ask chatgpt as well to see if that can help or you can post your question on stackoverflow.com they give very legitimate answers and they are very quick.

    • @priyankabachhav5315
      @priyankabachhav5315 ปีที่แล้ว

      @@learnssis actually I google it I implemented various ways also, I tried chatgpt but error still arrives.. And also I tried this csv part but it's not taking my folder path accurately.. I am getting error there it's unable to find file. Can we connect and then I will show you?? Maybe you will get to know.. What I am doing wrong

    • @learnssis
      @learnssis  ปีที่แล้ว +1

      @@priyankabachhav5315 Actually I don't have much experience with it thus not sure if I can help you with it, please share the screenshot of the error message and code to aqil33@gmail.com. It might be related to permission issue. Make sure that your sql services owner should have full access on the folder and file that you are trying to import.

  • @1321vishal
    @1321vishal ปีที่แล้ว

    thank you so much sir.

    • @learnssis
      @learnssis  ปีที่แล้ว

      You are most welcome.

  • @sashafroyland
    @sashafroyland ปีที่แล้ว

    Is using Azure SQL Server, then file needs to be stored on Blog Storage, otherwise it will error out.

    • @learnssis
      @learnssis  ปีที่แล้ว

      Frankly speaking I never used bulk insert query with Azure, I mostly tried with On-Premises instance.
      I saw some examples online where they use C# to read data from a flat file insert the data to Azure sql server
      stackoverflow.com/questions/46004119/increase-sql-bulk-copy-speed-on-azure
      stackoverflow.com/questions/51686757/bulkcopy-100-million-rows-of-data-to-azure-sql-server-using-c

  • @kannann1492
    @kannann1492 2 ปีที่แล้ว

    Hey i am getting error. I am using mysql workbench
    10:40:18 BULK INSERT tripdata FROM 'D:\Sample Data\Raw data for Capital Bikeshare\202004.csv' WITH (FORMAT = 'CSV' , FIRSTROW=2 , FIELDTERMINATOR = ',' , ROWTERMINATOR = '0x0a')
    Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BULK INSERT tripdata FROM 'D:\Sample Data\Raw data for Capital Bikeshare\202004.' at line 1 0.047 sec

    • @learnssis
      @learnssis  2 ปีที่แล้ว +1

      You can try this query
      LOAD DATA INFILE 'D:/Files/Sellers.CSV' INTO TABLE sellers
      FIELDS TERMINATED BY ','
      OPTIONALLY ENCLOSED BY '"'
      LINES TERMINATED BY '
      '
      IGNORE 1 ROWS;
      I will make a video on this very soon.

    • @kannann1492
      @kannann1492 2 ปีที่แล้ว

      @@learnssis That also not working Because I am working in MYSQL work bench. My CSV File local in my local computer when I try to upload I getting
      Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement 0.015 sec

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      @@kannann1492 Check this video
      th-cam.com/video/bG08P9nNLcQ/w-d-xo.html
      And the query that I shared with you, I tested it in mysql workbench and it is working fine, I will try to make a video on this today.

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      @@kannann1492 you can watch the video here how to import a csv file into mysql using a bulk insert query
      th-cam.com/video/HlnmXv_kCL0/w-d-xo.html

  • @hdwallpaper7492
    @hdwallpaper7492 2 ปีที่แล้ว

    nice tutorial ...!! I have diffferent CSV file and throwing me error of (Bulk load data conversion error (type mismatch or invalid character for the specified codepage)) could u please suggest

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      I would suggest you to try using Data Flow task and see if you are able to load it from there.

  • @apeanicaisegneki4200
    @apeanicaisegneki4200 ปีที่แล้ว

    Thanks Abdul .but I have csv file from kaggle but having difficulties to load data from these csv file into my SQL table using SSIS

    • @learnssis
      @learnssis  ปีที่แล้ว

      Try using data flow task and if that fails then paste the error here I will try to help
      th-cam.com/video/aMhwCepVpwE/w-d-xo.html

  • @nareshMerugu
    @nareshMerugu 7 หลายเดือนก่อน

    Sir,we have import the multiple CSV files in one goo is it possible??

    • @learnssis
      @learnssis  7 หลายเดือนก่อน

      I think you can try using cursor and pass one file name at a time to the import sql query and see how it works.

    • @nareshMerugu
      @nareshMerugu 7 หลายเดือนก่อน

      @@learnssis tq for ur reply...but I tried same as but one column doesn't show the data it's showing only data retrieved successfully

    • @nareshMerugu
      @nareshMerugu 7 หลายเดือนก่อน

      How it is possible sir ?

    • @learnssis
      @learnssis  7 หลายเดือนก่อน

      @@nareshMeruguCan you ask chatgpt to write the sql query for you ? It can write the sql query for you, just give him instruction.

    • @learnssis
      @learnssis  7 หลายเดือนก่อน

      @@nareshMerugu
      The below code that I got from google it works for importing multiple csv files, I tested it with 2 files and it worked.
      Provide your folder path at this line
      SET @path = 'D:\Files\'
      And the provide your sql table name at this line
      set @sql = 'BULK INSERT YourTableName FROM ''' + @path +
      stackoverflow.com/questions/16076309/import-multiple-csv-files-to-sql-server-from-a-folder
      EXEC sp_configure 'show advanced options', 1;
      RECONFIGURE;
      EXEC sp_configure 'xp_cmdshell', 1;
      RECONFIGURE;
      go
      --BULK INSERT MULTIPLE FILES From a Folder
      drop table if exists ALLFILENAMES
      --a table to loop thru filenames drop table ALLFILENAMES
      CREATE TABLE ALLFILENAMES(WHICHPATH VARCHAR(255),WHICHFILE varchar(255))
      --some variables
      declare @filename varchar(255),
      @path varchar(255),
      @sql varchar(8000),
      @cmd varchar(1000)
      --get the list of files to process:
      SET @path = 'D:\Files\'
      SET @cmd = 'dir ' + @path + '*.csv /b'
      INSERT INTO ALLFILENAMES(WHICHFILE)
      EXEC Master..xp_cmdShell @cmd
      UPDATE ALLFILENAMES SET WHICHPATH = @path where WHICHPATH is null
      --cursor loop
      declare c1 cursor for SELECT WHICHPATH,WHICHFILE FROM ALLFILENAMES where WHICHFILE like '%.csv%'
      open c1
      fetch next from c1 into @path,@filename
      While @@fetch_status -1
      begin
      --bulk insert won't take a variable name, so make a sql and execute it instead:
      set @sql = 'BULK INSERT YourTableName FROM ''' + @path + @filename + ''' '
      + ' WITH (
      FIELDTERMINATOR = '','',
      ROWTERMINATOR = ''
      '',
      FIRSTROW = 2
      ) '
      print @sql
      exec (@sql)
      fetch next from c1 into @path,@filename
      end
      close c1
      deallocate c1

  • @vishnupriya407
    @vishnupriya407 ปีที่แล้ว

    Can we bulk insert delta table like this from blob storage?

    • @learnssis
      @learnssis  ปีที่แล้ว

      I have never tried that, you can try it out.

  • @rika0729
    @rika0729 6 หลายเดือนก่อน

    whats your current salary... in hand LPA?

  • @TheMadyam
    @TheMadyam 2 ปีที่แล้ว

    Great video and series. Can you do a video on how to run a SSRS report with a parameter from SSIS and get the report and move to a folder?

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      I have not done this kind of thing as I did not get a chance to work on SSRS reports. Also I don't have SSRS installed on my machine at the moment. Sorry for this.

  • @gobakos7890
    @gobakos7890 11 หลายเดือนก่อน

    Do you know which tables contains the products in a woocommerce store?also do you have video for XML ?Thanks and you got a sub

    • @learnssis
      @learnssis  11 หลายเดือนก่อน

      Sorry I don't have any information about woocommerce store. I have a video below on loading data from xml file to sql server table using sql query
      th-cam.com/video/w05oX0obgp4/w-d-xo.html
      This is how we can import xml file to sql server table using ssis
      th-cam.com/video/9vWYLGsaXJc/w-d-xo.html
      And this the method how we can export data from sql server to xml using ssis
      th-cam.com/video/NuT3CpiUSrM/w-d-xo.html

  • @camelCased
    @camelCased ปีที่แล้ว

    It works for simple cases, but don't even think to mess with CSVs and Microsoft SQL server for data that might contain quotes or newlines - it just will break in one way or another. SQL server is not able nor export nor import ISO standard compatible CSV files.

    • @learnssis
      @learnssis  ปีที่แล้ว

      For complex cases, try to use SSIS which is built for large data processing.

    • @camelCased
      @camelCased ปีที่แล้ว

      @@learnssis Unfortunately, if using SSIS to export to CSV files, it will have the same problems with data that has quotes and newlines. There are two great articles about these issues; I cannot link them directly (youtube is aggressive against links lately), but you can find them by names:
      'Comedy Limited' with SQL Server
      Extracting Data from (Azure) SQL Server Huge Tables in RFC 4180-Compliant CSV Files
      Really amusing and sad stories.

    • @learnssis
      @learnssis  ปีที่แล้ว

      @@camelCased Correct. We can use Fixed width files to export data to flat files if data is huge and contains double quotes or newline characters.

  • @RaviKumar-tp6ii
    @RaviKumar-tp6ii 2 ปีที่แล้ว

    how to pass a file to import dynamically?

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      Hi Ravi, you won't be able to pass the file dynamically to Bulk insert task. I have shown other options how you can move multiple sql tables from one server to another
      How to transfer multiple sql tables from one server to another
      th-cam.com/video/YXeEZsGmJrI/w-d-xo.html
      How to dynamically move multiple sql tables from one server to another
      th-cam.com/video/-GEShPKmlTw/w-d-xo.html

  • @foodlovers76
    @foodlovers76 2 ปีที่แล้ว

    How to load multiple Excel files into SQL server by using bulk insert

  • @Deepratan27
    @Deepratan27 2 ปีที่แล้ว

    How to do this in PL/SQL

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      If you want to do this in MySQL then you take a look at this video
      th-cam.com/video/HlnmXv_kCL0/w-d-xo.html

    • @Deepratan27
      @Deepratan27 2 ปีที่แล้ว

      @@learnssis Error starting at line : 6 in command -
      FIELDS TERMINATED BY ','
      Error report -
      Unknown Command

    • @Deepratan27
      @Deepratan27 2 ปีที่แล้ว

      Not working

    • @learnssis
      @learnssis  2 ปีที่แล้ว

      @@Deepratan27 what is your source and destination and on which platform are you executing this query ?