Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 how to backup a single table to CSV using SQL

Author  Topic 

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-06-02 : 11:09:37
I need to backup a SQL server table to .CSV or .txt with any name+startdate that is saved to a location on our network

How dowe script for this

Thanks
Jim

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-02 : 11:21:49
Something like this:


DECLARE @table VARCHAR(128), @file VARCHAR(255), @cmd VARCHAR(512)
SET @table='myTable'
SET @file = '\\computer\path\' + @table + '_' + CONVERT(CHAR(8),GETDATE(),112) + '.csv'
SET @cmd = 'bcp "dbname..' + @table + '" out "' + @file + '" -S. -T -c -t,'
EXEC master..xp_cmdshell @cmd
Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-06-02 : 12:01:22
Great.Thanks Much
Initially it failed as sys.xp_cmdshell is not enabled
After googling and enabling it it worked an created a backup in the network location
Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-06-06 : 10:31:51
Hey
I was told that I will not be given permissions to execute master..xp_cmdshell @cmd in our environment
Is there a way to do the same using using anything else
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-06-06 : 10:45:52
if this is just a one-off kind of thing and you don't need to do this inside the SQL Server, just issue the BCP command at your local machine or at Server machine command prompt.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-06 : 11:13:07
You can create a job that has an Operating System step, then run it using sp_start_job. The problem is that a job can't accept parameters. You can however load the parameters into a regular table and have the step read them using bcp or osql to a file.

Create a table in your database to hold parameters:

create table params(tablename varchar(128) not null primary key)

Create a stored procedure that can accept a table name:

create procedure ExportTable @table varchar(128) as
set nocount on
truncate table params
insert params values(@table)
exec msdb..sp_start_job @job_name='Export Table'


In Notepad or similar program, copy/paste the following text (change the db reference to match your database name):
rem parse date to use in filename
for /F "tokens=2-4 delims=/ " %%a in ("%date%") do @set dt=%%c%%a%%b

rem extract table name from params table
for /F "tokens=1 delims=" %%z in ('sqlcmd -S. -E -W -h-1 -s"" -Q"set nocount on;select top 1 tablename from db..params"') do @set table=%%z

rem construct bcp filename
set bcpfile="\\computer\path\%table%_%dt%.csv"

rem run bcp
bcp "db..%table%" out %bcpfile% -S. -T -c -t,
Save the file with a .BAT extension, make sure .TXT is not appended automatically. (e.g. c:\exporttable.bat)

Create a job named "Export Table" and add an Operating system step consisting of:
c:\exporttable.bat


You can save the batch file anywhere, just make sure the SQL Agent service account has permissions to its directory. Then execute your stored procedure like so:

exec ExportTable 'myTable'
Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-06-06 : 12:15:47
Iam not sure whether I have done all the steps you have mentioned
I get the below error when I execute.

The specified @job_name ('Export Table') does not exist.
Thanks

quote:
Originally posted by robvolk

You can create a job that has an Operating System step, then run it using sp_start_job. The problem is that a job can't accept parameters. You can however load the parameters into a regular table and have the step read them using bcp or osql to a file.

Create a table in your database to hold parameters:

create table params(tablename varchar(128) not null primary key)

Create a stored procedure that can accept a table name:

create procedure ExportTable @table varchar(128) as
set nocount on
truncate table params
insert params values(@table)
exec msdb..sp_start_job @job_name='Export Table'


In Notepad or similar program, copy/paste the following text (change the db reference to match your database name):
rem parse date to use in filename
for /F "tokens=2-4 delims=/ " %%a in ("%date%") do @set dt=%%c%%a%%b

rem extract table name from params table
for /F "tokens=1 delims=" %%z in ('sqlcmd -S. -E -W -h-1 -s"" -Q"set nocount on;select top 1 tablename from db..params"') do @set table=%%z

rem construct bcp filename
set bcpfile="\\computer\path\%table%_%dt%.csv"

rem run bcp
bcp "db..%table%" out %bcpfile% -S. -T -c -t,
Save the file with a .BAT extension, make sure .TXT is not appended automatically. (e.g. c:\exporttable.bat)

Create a job named "Export Table" and add an Operating system step consisting of:
c:\exporttable.bat


You can save the batch file anywhere, just make sure the SQL Agent service account has permissions to its directory. Then execute your stored procedure like so:

exec ExportTable 'myTable'

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-06 : 12:29:22
You have to create the job manually. In SSMS, right click SQL Agent, expand the Jobs folder, right-click and choose "New Job". Just make sure the name matches the name you use in sp_start_job.
Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-06-06 : 12:43:06
This is going to be a recurring thing and I was trying to create a package using ssis

quote:
Originally posted by khtan

if this is just a one-off kind of thing and you don't need to do this inside the SQL Server, just issue the BCP command at your local machine or at Server machine command prompt.


KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2011-06-06 : 13:06:53
I created a job in SSMs and I was able to execute.It shows that Job 'Export Table' started successfully in SSMS but it does not create the output file

Below is the .Bat file script

rem parse date to use in filename
for /F "tokens=2-4 delims=/ " %%a in ("%date%") do @set dt=%%c%%a%%b

rem extract table name from params table
for /F "tokens=1 delims=" %%z in ('sqlcmd -S. -E -W -h-1 -s"" -Q"set nocount on;select top 1 tablename from db..params"') do @set table=%%z

rem construct bcp filename
set bcpfile="\\nas700ISP\WAS_FSA\%table%_%dt%.csv"

rem run bcp
bcp "Underwriting..%table%" out %bcpfile% -S. -T -c -t,



quote:
Originally posted by robvolk

You have to create the job manually. In SSMS, right click SQL Agent, expand the Jobs folder, right-click and choose "New Job". Just make sure the name matches the name you use in sp_start_job.

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-06-06 : 13:46:10
Check the job step history and see if there were any messages or errors. You should also include step output in history (Advanced tab of job step properties). Make sure SQLAgent is not running under the LocalSystem account, otherwise it cannot write to a UNC path. You'll have to use a Windows domain account with the necessary permissions on the computer being written to.

I would recommend bcp'ing to a local folder and running a copy job to the remote system. It's a little more complicated but it avoids job failure or other issues if the network hiccups or is otherwise unavailable when the export job runs. (I apply the same logic to database backups)
Go to Top of Page
   

- Advertisement -