Hello everybody I have some certs in SQL 2008 but am by no means a master. I was curious if my goal was to query a CSV or Excel file on my desktop within SSMS how to do this? BEFORE anyone mentions Openrowset and turning it on through sp_configure I have tried that and it seems to not working when trying ‘Microsoft.Jet.OLEDB.4.0’ driver, the ‘MSADSAL’ drivers or the other iterations of it. It gives some error about the server not being allowed to be null OR else that this is only able to run in Single threaded mode. I read up on this and a lot of people claim that you have to run a 32 bit version or similar to get it. What confuses me is I can use the ‘BULK’ method with Openrowset and then select the ‘Single_Clob’ however I get a comma seperated string which I would then have to parse out.
What my real question is, is there a simple way to just query a CSV or Excel file fast in a 64 bit version of SSMS 2008 R2?
Figured it out:
Approach A: bcp:
Ensure that xp_cmdshell is on. (If you are on production environment for an enterprise company this MAY not be an option for security reasons. You may always use a BETA or QA environment to put the data to first and hopefully have linked servers set up).
exec sp_configure ‘xp_cmdshell’, 1
reconfigure
Create a csv file for testing purposes. I did a three column csv flat file as such:
A, Brett, 1
B, John, 2
C, Brian, 3
Create a temp or permanent table matching the data types shown here:
create table Test ( value varchar(3), Name varchar(16), ID int)
Run bcp from the command shell ensuring you do not have the file open.
EXEC xp_cmdshell ‘bcp Test..Test in “C:\test\Test.txt” -c -t , -r \n -T’
GO
if you are curious on switches with bcp, use the help file: EXEC xp_cmdshell ‘bcp /?’
! For myself I had this method work in SQL 2005 and FAIL in SQL 2008 and 2008 R2 with this exception: Error = [Microsoft][SQL Native Client]Unexpected EOF encountered in BCP data-file. I looked and looked and looked but it appears that my file in standard UTF format with the standard comma delimeter and the standard \n line feed SQL bcp does not like. However my next solution worked right away.
Approach B: Bulk Insert:
Follow steps 2 and 3 above the same
Run Bulk Insert as such:
BULK INSERT dbo.Test
FROM ‘C:\test\Test.csv’
WITH (FIELDTERMINATOR = ‘,’, ROWTERMINATOR = ‘\n’)
As long as your table data types match the import, you should be fine. Ulimtately a simple table creation and a three line statement to me is pretty quick compared to having to run a wizard constantly or open up Business Intelligence Development Studio at which point I might as well just a write a custom C# app.