Avatar of Dan Henery
Dan Henery
Flag for United States of America asked on

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?
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
Qlemo

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.
Dan Henery

ASKER
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
ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dan Henery

ASKER
Thank you Qlemo!!
Would you happen to have an example of how I would do this using PowerShell?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Qlemo

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

Dan Henery

ASKER
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"
Qlemo

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Dan Henery

ASKER
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
Qlemo

Your SQL is not correct, Try with a simple SQL first.
Dan Henery

ASKER
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"
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Qlemo

Try that line alone. It should prompt for input. And check that the path exists, including subfolders.
Dan Henery

ASKER
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('"','') } |
Qlemo

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.