I have a script that will run a sql query and if results are returned export those results to a csv file. I need to take that a step further now and have something similar to this: -- Run Query 1, if it returned results save. Run Query 2, if it returned results save, Run Query 3, if it returned results save.
$server = "Main"
$database = "hellfire"
$query1 = "Select * from mousetrap"
$query2 = "Select * from mickey"
$query3 = "Select * from brimstone"
$templatefile = @"C:\book1.csv"
$savedirectory = @"C:\ExportedFromSQL\Results\"
#Actual Conenction To SQL Server Goes here
#for brevity I am omitting
#check if query returned results
if ($SqlAdapter.Fill($DataSet) - ge 1)
{
#this is where my catch comes in
#if $query1 was executed I want to save the file as mouse
#if $query2 was executed I want to save the file as mi
#if $query3 was executed I want to save the file as stone
#$DataSet.Tables[0] | Export-Csv $templatefile -NoTypeInformation
}