Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export table into a file? 2

Status
Not open for further replies.

ranchan02

Technical User
Nov 1, 2005
13
US
Hi Everyone:

I created a stored procedure that updates records in the master table (we'll call it master_table) with data from another table (we'll call it update_table). A feature I want to add to my stored procedure is to export the data from the update_table into a file (csv, xls, etc) and once the file has been created, delete the update_table from the database. How do I go about this? Here's the specs of the environment: SQL Server 2000 running on Windows Server 2003.
 
here ia 1 example of copying data to a file by using bcp and xp_cmdshell

master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout c:\Authors.txt -c '

once you run this a file should be created on you c drive named Authors.txt

Denis The SQL Menace
SQL blog:
Personal Blog:
 
SQlDenis,

That would make a great tip. I used to use the spool cmd with Oracle but about gave up on SqlServer. It works great

 
Hi SQLDenis:

Thanks for the tip! I have a question though...in your example it creates this text file into the c: directory, but what would happen if the file already exist in the directory? does it overwrite it? I would prefer that if the file already exist, another output file (leaving the original one intact). How would I go about that?
 
Hi SQLDenis:

I tried doing a quick test with the sample you posted and I keep getting an error:

output
-------------------------------------------------------------------------
Password:
SQLState = S0002, NativeError = 208
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'PART_UPDATING_CNT'.
SQLState = 37000, NativeError = 8180
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared.
NULL

Here's the portion from my sp:

My SP
...
EXEC master..xp_cmdshell 'bcp "SELECT * FROM UPDATE_TABLE" queryout c:\Authors.txt -c '
...

Am I doing something wrong?

 
Hi SQLDenis:

Okay i changed it up and it works now...but when I tried to change it and save it to a network path, it gives me this error message:

output
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Password:
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC SQL Server Driver]Unable to open BCP host data-file
NULL

Here's the line:

EXEC master..xp_cmdshell 'bcp "SELECT * FROM database.dbo.update_table" queryout \\machine_name\shared_folder\test.xls -c '

Am I missing something?

P.S. Thank you for your help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top