Link to home
Create AccountLog in
Avatar of Dan Henery
Dan HeneryFlag 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?
Avatar of Qlemo
Qlemo
Flag of Germany image

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.
Avatar of 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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you Qlemo!!
Would you happen to have an example of how I would do this using PowerShell?
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

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"
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.
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
Your SQL is not correct, Try with a simple SQL first.
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"
Try that line alone. It should prompt for input. And check that the path exists, including subfolders.
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('"','') } |
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