0

Within a stored procedure, I need to take a whole CSV file as a string, then pick out all the values in one "column" to do a further query on the database.

I cannot use a saved doc - so i think that rules out openrowset, and the whole thing has to be done within a stored procedure.

Have spent hours googling and trying, but can find a good answer. One possible was http://www.tainyan.com/articles/entry-32/converting-csv-to-sql-data-table-with-stored-procedure.html but it doesnt work and i can find the error.

How should this be done please?

5
  • 1
    What version of sql server are you using? Commented Aug 17, 2012 at 11:44
  • where is the csv file stored? is it in the file system or in database? Commented Aug 17, 2012 at 11:45
  • This is better done on the application side, not SQL Server (barring SSIS for data import). Commented Aug 17, 2012 at 11:45
  • Sounds like it forms part of a wider application. Have a look at Bulk inserting the CSV data into a separate table and going for there. In .Net this would be using SQLBulkCopy Commented Aug 17, 2012 at 11:46
  • Believe me, I would much rather being using C# / LINQ, but i need to do it on the database. The string comes into the server via a web service. I am using SQL Server 2005 Commented Aug 17, 2012 at 12:03

1 Answer 1

1

I don't really like this but it will work, provided your csv column remains at the same column index. I'd be wary of the performance of this but it might work.

See Fiddle here: http://sqlfiddle.com/#!3/336b7/1

Basically convert your csv file to xml, cast to an xml type, then perform queries on the xml.

Sign up to request clarification or add additional context in comments.

1 Comment

I have marked this as the answer, as that would work. In the end, i had to use this split function blog.logiclabz.com/sql-server/… .- Split the csv into rows, insert them as a text column into a temp table, loop through that table, split the info and load it into another temp table. It's pretty ugly (hence i haven't posted it), but it just about does the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.