Sophonias
Sophonias

Reputation: 914

How to insert data from a CSV file into SQL Server?

I wanted to know if there is any way of accessing my local hard drive to use BULK INSERT or another command to insert data into a SQL Server database table from a CSV file.

I've used BULK INSERT before but in order to use it, I had to put the CSV file on the server's hard drive, not my local hard drive. I had access to the hard disk of the computer the server was on.

This time I only have read and write access to SQL Server but not to the whole computer it is installed on.

Does anyone have any suggestions on how I can do this?

Upvotes: 1

Views: 1503

Answers (2)

Sophonias
Sophonias

Reputation: 914

I managed to solve my problem. I created a temporary database on my local server and did a 'BULK INSERT' to a temporary table locally. I then linked my local server to the server I wanted to query. Finally, I got the inner join I wanted between the temporary table(where I bulk inserted my data into) and the main DB.

see here to on how to create DB links

See also the first answer to this question on how to write an inner join statement on how to write an inner join between two servers.

Upvotes: 0

Aaron Bertrand
Aaron Bertrand

Reputation: 280510

If you want to use BULK INSERT then you need to get the file to a place that SQL Server can see (a file share, perhaps). Or perform the BULK INSERT on a different server and then moving the data over to the other SQL Server via a linked server, SSIS, etc.

Of course CSV is not exactly a complex format to handle - you can easily do this from a simple C# application by either decomposing the CSV into a set and using table-valued parameters or using SqlBulkCopy.

Upvotes: 3

Related Questions