T-SQL Bulk Insert From Text (TXT) File Into A Temporary Table

First, have a text file with some values you would like to BULK INSERT.
The example below is a text file called ‘animals.txt’ with contents of:

 cat
 dog
 fish
 bird
 snake

The SQL snippet:

 CREATE TABLE #TempTable (Pets CHAR(16));
 BULK INSERT #TempTable
 FROM 'C:\SQLTesting\animals.txt'
 WITH
 (
 DATAFILETYPE= 'char',
 FIELDTERMINATOR=',',
 ROWTERMINATOR='\r' -- Carriage Return
 );
 SELECT Pets FROM #TempTable;
 -- Remove our temporary table.
 DROP TABLE #TempTable;

CREATE TABLE:
https://msdn.microsoft.com/en-us/library/ms174979.aspx

BULK INSERT:

https://msdn.microsoft.com/en-us/library/ms188365.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s