Solved

MS SQL export CSV & schedule It

Posted on 2016-10-25
9
60 Views
Last Modified: 2016-11-15
I just have a simple query that I want to run 5:00 pm PST Daily and export the data in csv format.
Can you show me the easiest way using SQL Server agent?
0
Comment
Question by:ITsolutionWizard
9 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41859752
You can create a SQL Job with the below code..

DECLARE  @Command NVARCHAR(4000)=''
	
SET @Command = 'bcp " ' + 'SELECT CreditReport FROM [PAWAN].[ABCInvestment].data.CreditReport WHERE 1 = 1 ' + '"' + ' queryout "E:\Pawan' +  '.csv' + '"' + ' -T -N'
 
PRINT @Command
 
EXEC xp_cmdshell   @Command

Open in new window

0
 
LVL 4

Accepted Solution

by:
Daniel Jones earned 500 total points
ID: 41859760
You can set up an SQL server agent job of type cmdexec to execute the bcp utility.
First try the bcp command by itself and remember to specify the -c argument for char instead of the default bin.


Wrap this command into a scheduled job
EXEC  master..xp_cmdshell 'SQLCMD -S server\SQLSERVERDEV2005 -E -Q "SELECT TOP 10 * FROM pubs.dbo.authors" -b -o c:\myoutput.txt', no_output
0
 

Author Comment

by:ITsolutionWizard
ID: 41859761
oh sorry. I just find out our sql does not have sql job agent feature.
what is our option?
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41859773
You can enable it, Right click on the SQL Server Agent and start the service.
0
 

Author Comment

by:ITsolutionWizard
ID: 41859776
It does not have it. This is azure and I do not know why we do not have it
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41859779
try this MSDN link... It has steps you need...

https://blogs.msdn.microsoft.com/ms-tech_learning_with_prasant/2014/07/09/scheduling-job-on-sql-azure/

or you may need to create a windows service..

Hope it helps !!
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 41859923
It does not have it. This is azure and I do not know why we do not have it
Can't you do it in the opposite direction? In the target server (where you want the .csv file to be stored) schedule a task to export the data from Azure.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41871741
Hi ITsolutionWizard,
Appreciate feedback on this?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41884414
Hi,

Is this done ?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question