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 networkHow dowe script for thisThanksJim |
|
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 |
 |
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2011-06-02 : 12:01:22
|
Great.Thanks MuchInitially it failed as sys.xp_cmdshell is not enabledAfter googling and enabling it it worked an created a backup in the network location |
 |
|
jim_jim
Constraint Violating Yak Guru
306 Posts |
Posted - 2011-06-06 : 10:31:51
|
HeyI was told that I will not be given permissions to execute master..xp_cmdshell @cmd in our environmentIs there a way to do the same using using anything else |
 |
|
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] |
 |
|
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) asset nocount ontruncate table paramsinsert 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 filenamefor /F "tokens=2-4 delims=/ " %%a in ("%date%") do @set dt=%%c%%a%%brem extract table name from params tablefor /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=%%zrem construct bcp filenameset bcpfile="\\computer\path\%table%_%dt%.csv"rem run bcpbcp "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' |
 |
|
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 mentionedI get the below error when I execute.The specified @job_name ('Export Table') does not exist.Thanksquote: 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) asset nocount ontruncate table paramsinsert 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 filenamefor /F "tokens=2-4 delims=/ " %%a in ("%date%") do @set dt=%%c%%a%%brem extract table name from params tablefor /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=%%zrem construct bcp filenameset bcpfile="\\computer\path\%table%_%dt%.csv"rem run bcpbcp "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'
|
 |
|
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. |
 |
|
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 ssisquote: 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]
|
 |
|
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 fileBelow is the .Bat file scriptrem parse date to use in filenamefor /F "tokens=2-4 delims=/ " %%a in ("%date%") do @set dt=%%c%%a%%brem extract table name from params tablefor /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=%%zrem construct bcp filenameset bcpfile="\\nas700ISP\WAS_FSA\%table%_%dt%.csv"rem run bcpbcp "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.
|
 |
|
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) |
 |
|
|