Only import 10 rows from a text file

Product:

Microsoft SQL server 2016

Issue:

How to test import only 10 rows from a text file, with bulk insert command? To check if it works.

Solution:

BULK INSERT Salestable
FROM 'C:\temp\data.txt'
WITH (LASTROW = 10,
    BATCHSIZE=250000,
    MAXERRORS=2);

Enter LASTROW = 10 to only read ten rows of data from your data.txt file. Then you can check if you get the correct type of data to your table.

LASTROW = last_row

Specifies the number of the last row to load. The default is 0, which indicates the last row in the specified data file.

MAXERRORS = max_errors

Specifies the maximum number of syntax errors allowed in the data before the bulk-import operation is canceled. Each row that can’t be imported by the bulk-import operation is ignored and counted as one error. If max_errors isn’t specified, the default is 10.

BATCHSIZE = batch_size

Specifies the number of rows in a batch. Each batch is copied to the server as one transaction. If this fails, SQL Server commits or rolls back the transaction for every batch. By default, all data in the specified data file is one batch.

If you cancel a BULK INSERT, it will try to roll back all data, this will take time.

 

More information:

https://learn.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql?view=sql-server-ver16#performance-considerations

https://www.mssqltips.com/sqlservertip/6109/bulk-insert-data-into-sql-server/