June 12, 2008 at 9:36 am
I need to be able to import a CSV file into a table via a stored procedure. It would be nice if I could pass the file name into the stored procedure as well as a parameter. Is XML my best option? For various reasons, I do not want to use DTS or Bulk. I will also need to know how to export a csv file from a stored procedure as well. I am using SQL 2000. Any suggestions would be greatly appreciated.
Stacy
June 12, 2008 at 11:36 am
OpenQuery will allow you to do this.
June 12, 2008 at 9:13 pm
Michael Earl (6/12/2008)
OpenQuery will allow you to do this.
Do you have example code of how to use OpenQuery to export to a CSV File?
--Jeff Moden
Change is inevitable... Change for the better is not.
June 13, 2008 at 3:59 am
June 16, 2008 at 8:31 am
I'm looking for something that is very portable. I have a csv file that needs to be uploaded to a sql table, then later after much manipulation, be exported from a sql table to a new csv file. It would be preferred if I could pass the file name into the stored procedure as a parameter. I realize there are other options for importing/exporting, but I would like to be able to do it within a stored procedure.
I'm not sure what other circumstances you are looking for?? Let me know if I have not answered your question, so that you can answer mine?
Thank you,
Stacy
June 16, 2008 at 8:57 am
OpenQuery will do import or export? or both? Do you have an example? I've looked it up, and unfortunately, am still confused.
Thank you,
Stacy
June 16, 2008 at 10:19 am
stacya (6/16/2008)
OpenQuery will do import or export? or both? Do you have an example? I've looked it up, and unfortunately, am still confused.Thank you,
Stacy
Openquery just does IMPORT.
* Noel
June 17, 2008 at 10:15 am
Jeff Moden (6/12/2008)
Michael Earl (6/12/2008)
OpenQuery will allow you to do this.Do you have example code of how to use OpenQuery to export to a CSV File?
Jeff, Michael might have intent for OPENROWSET. I have got some code like:SELECT *
INTO db1.dbo.table1
FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\book1.xls',
'SELECT * FROM [sheet1$]')
I guess (right now unable to test as right now I am not on SQL server machine 🙁 ) it will support CSV as well.
Mahesh
MH-09-AM-8694
June 17, 2008 at 10:20 am
[font="Verdana"]
stacya (6/12/2008)
I need to be able to import a CSV file into a table via a stored procedure. It would be nice if I could pass the file name into the stored procedure as well as a parameter. Is XML my best option? For various reasons, I do not want to use DTS or Bulk. I will also need to know how to export a csv file from a stored procedure as well. I am using SQL 2000. Any suggestions would be greatly appreciated.Stacy
why don't you try Bulk insert in such case? Like:
BULK INSERT [orders]
FROM ' '
Mahesh
[/font]
MH-09-AM-8694
June 17, 2008 at 11:02 pm
OpenRowSet will not create a new file. It can be used to populate an existing file. Same goes with a text based linked server.
Bulk Insert will certainly do an Import but it will not export.
There is no "portable" way to do this amongst the RDBMS's that you mention. You need to wrap the functionality for each database type in a common interface and that's about as close as you're going to get.
The only way to create a new file from a stored proc in SQL server is to call a DTS job, make a trip to xp_CmdShell, or use SP_OA* methods... most of these methods require SA privs.
Of course, you could always run a batch job with calls to either BCP or OSQL... I believe OSQL is known as SQLCmd in 2005 (someone correct me, please).
Or, you can write an app or use something like "Hibernate"...
--Jeff Moden
Change is inevitable... Change for the better is not.
September 22, 2008 at 7:43 am
Don't know if its too late or not but here is an article that shows how to export data to csv from within a sql script.
http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply