SQL Query Output to file

I am running MSSQL 2012 on Windows 2008
I have a SQL Query that is run on a daily basis and outputs to a Text file. This is done by a Batch file that uses SQLCMD similar to the following ; sqlcmd -S %SERVERNAME% -d DATABASE -s "|" -m1 -W -h-1 -i J:\Folder1\Query1.sql  -o J:\Folder1\Output\Results_%DATE:~4,2%_%DATE:~7,2%_%DATE:~-4%.txt

This Batch file is kicked off using Windows Task Scheduler. Once it completes another batch file is scheduled to move the file from the local folder to a shared folder on another server.

The SQL Query Batch is beginning to run long and past the start time of the Move Batch. I have adjusted the start times to fix this, but I do not want to keep doing this.

What I would like to do, if possible, is leverage SQL Server Agent to schedule a job in SSMS the Runs the Query and Outputs the results directly to the Shared folder on the other server. Is this possible? If so How?
LVL 1
DKHeneryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Why not modifying the path of the sqlcmd to point to the final destination? The only thing you need to care for is that the scheduled task runs as an user, no system account, so network access is available. Paths may not contain mapped drive letters, only UNC.
1
DKHeneryAuthor Commented:
I thought of that as well, but I would like to get away from using Windows Task Scheduler and Batch files altogether. I may end up doing that in the end if I cannot find a way to accomplish this within SSMS
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Of course you can do with SQL Agent jobs:
1. You can run the existing batch file from SQL Agent instead of Task Scheduler.
2. You can use a PowerShell script instead, and run that as SQL Agent Job.
3. However, to do it completely inside of MSSQL, you'll need to create a SSIS (DTS) package, and run that one. The output of T-SQL jobs in SQL Agent cannot be formatted like with sqlcmd, and hence can only be used for logging.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

DKHeneryAuthor Commented:
Thank you Qlemo!!
Would you happen to have an example of how I would do this using PowerShell?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
A minimalistic template in PowerShell, assuming your SQL file only contains a single SQL:
$svr='SqlServer'
$db = 'Database'
$cmd = Get-Content J:\Folder1\Query1.sql

$constr = "server=$svr;database=$db;Integrated Security=sspi"
(New-Object System.Data.SqlClient.SqlDataAdapter($cmd, $constr)).Fill($set) | out-null
$set.Tables[0] |
  convertTo-CSV -noType -delimiter '|' |
  % { $_.Replace('"','') } |
  Out-File -Encoding ASCII "J:\Folder1\Output\Results_$(get-date -format 'yyyy_MM_dd').txt"

Open in new window

Note that I've changed the date format to something you can sort for.
Instead of reading the SQL from a file, you could also code it directly:
$cmd = @"
select a, b, c
from tbl
where x = y
"

Open in new window

0
DKHeneryAuthor Commented:
Thanks for all your help, but I am having a problem getting this to work in PowerShell. When I created a SQL Server Agent Job it say Syntax Error Line 10. If I run it directly in PoweShell I get the following message about Ambiguous overloads:

PS C:\Users\Me> $svr='myserver'
PS C:\Users\Me> $db = 'mydb'
PS C:\Users\Me> $cmd = Get-Content J:\Folder1\Query1.sql
PS C:\Users\Me>
PS C:\Users\Me> $constr = "server=$svr;database=$db;Integrated Security=sspi"
PS C:\Users\Me> (New-Object System.Data.SqlClient.SqlDataAdapter($cmd, $constr)).Fill($set) | out-null
Multiple ambiguous overloads found for "Fill" and the argument count: "1".
At line:1 char:70
+ (New-Object System.Data.SqlClient.SqlDataAdapter($cmd, $constr)).Fill <<<< ($set) | out-null
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

PS C:\Users\Me> $set.Tables[0] |
>>   convertTo-CSV -noType -delimiter '|' |
>>   % { $_.Replace('"','') } |
>>   Out-File -Encoding ASCII "J:\Folder1\Output\Results_$(get-date -format 'yyyy_MM_dd').txt"
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Forgot to initialize $set pre filling it.
$svr='SqlServer'
$db = 'Database'
$cmd = Get-Content J:\Folder1\Query1.sql

$constr = "server=$svr;database=$db;Integrated Security=sspi"
$set = New-Object Data.DataTable
(New-Object System.Data.SqlClient.SqlDataAdapter($cmd, $constr)).Fill($set) | out-null
$set.Tables[0] |
  convertTo-CSV -noType -delimiter '|' |
  % { $_.Replace('"','') } |
  Out-File -Encoding ASCII "J:\Folder1\Output\Results_$(get-date -format 'yyyy_MM_dd').txt"

Open in new window

What the error in Sql Agent shall tell us, I don't know.
0
DKHeneryAuthor Commented:
New Error... is this referencing something within the SQL Query file?

Exception calling "Fill" with "1" argument(s): "Incorrect syntax near 'SELECT'."
At line:1 char:70
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Your SQL is not correct, Try with a simple SQL first.
0
DKHeneryAuthor Commented:
I am almost there.... getting a Syntax error on the last Line

Out-File -Encoding ASCII "J:\Fairwarning\Test\CHSLI_EPIC_$(get-date -format 'yyyy_MM_dd').txt"
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Try that line alone. It should prompt for input. And check that the path exists, including subfolders.
0
DKHeneryAuthor Commented:
I have run this a number of different ways... the issue lies somewhere within this part of the script;

$constr = "server=$svr;database=$db;Integrated Security=sspi"
$set = New-Object Data.DataTable
(New-Object System.Data.SqlClient.SqlDataAdapter($cmd, $constr)).Fill($set) | out-null
$set.Tables[0] |
  convertTo-CSV -noType -delimiter '|' |
  % { $_.Replace('"','') } |
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Does this work?
$svr='SqlServer'
$db = 'Database'
$cmd = Get-Content J:\Folder1\Query1.sql

$constr = "server=$svr;database=$db;Integrated Security=sspi"
$set = New-Object Data.DataTable
(New-Object System.Data.SqlClient.SqlDataAdapter($cmd, $constr)).Fill($set) | out-null
$set |
  convertTo-CSV -noType -delimiter '|' |
  % { $_.Replace('"','') } |
  Out-File -Encoding ASCII "J:\Folder1\Output\Results_$(get-date -format 'yyyy_MM_dd').txt"

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.