I would have a stored procedure that produce 7 result sets then write a Powershell function export data from SQL to 7 sheets in Excel and CSV in 1 shot. Below codes I downloaded the module ImportExcel from the link here
https://github.com/dfinke/ImportExcel
function Export-7Tables {
<#
.SYNOPSIS
This is summary of the function
.DESCRIPTION
This is a more detailed description
.PARAMETER
>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[string]$DBServerInstance='.\mssql14',
[Parameter(Mandatory=$false)]
[string]$DBName='MyDBName',
[Parameter(Mandatory=$false)]
[string]$DBSchema='dbo',
[Parameter(Mandatory=$false)]
[string[]]$DBStoredProcedure='StoredProcName',
[Parameter(Mandatory=$false)]
[string]$WorkSheetName1='Sheet1InExcel',
[Parameter(Mandatory=$false)]
[string]$WorkSheetName2='Sheet2InExcel',
[Parameter(Mandatory=$false)]
[string]$WorkSheetName3='Sheet3InExcel',
[Parameter(Mandatory=$false)]
[string]$OutputExcelFilePath = 'C:\Users\myPC\Documents\myExcelFile.xlsx',
[Parameter(Mandatory=$false)]
[string]$OutputCSVFilePath= 'C:\Users\myPC\Documents\myCSVFile.csv'
)
#$Verbose = $VerbosePreference -ne 'SilentlyContinue'
Try {
$ErrorActionPreference = 'Stop'
#Set Private Data from Psd1 to Shorter Variable
$PD=$MyInvocation.MyCommand.Module.PrivateData
$DBParams = @{
ServerInstance = $DBServerInstance
Database = $DBName
Credential = Import-Clixml -Path $PD.CredentialFile
Query = "EXEC [$DBName].[$DBSchema].[$DBStoredProcedure]"
}
#Call storedproc
$DataSet = Invoke-SqlCmd @DBParams -OutputAs DataSet
# assign the first result set of stored procedure to $Sheet1DataTable and so on so forth
$Sheet1DataTable = $DataSet.Tables[0]
$Sheet2DataTable = $DataSet.Tables[1]
$Sheet3DataTable = $DataSet.Tables[2]
#Filter just the properties we want, these would be column headers
$DesiredProperties1 = @(
'ColumnName1'
'ColumnName2'
'ColumnName3'
'ColumnName4'
'ColumnName5'
)
#Filter just the properties we want in $Sheet2DataTable
$DesiredPropertiesSheet2 = @(
'ColumnName1'
'ColumnName2'
'ColumnName3'
'ColumnName4'
'ColumnName5'
)
#Filter just the properties we want in $Sheet3DataTable
$DesiredPropertiesSheet3 = @(
'ColumnName1'
'ColumnName2'
'ColumnName3'
'ColumnName4'
'ColumnName5'
)
#writing headers for the first Excel sheet (Sheet1)
$ResultSP1 = $Sheet1DataTable | Select-Object -Property $DesiredPropertiesSheet1
$ResultSP2 = $Sheet2DataTable | Select-Object -Property $DesiredPropertiesSheet2
$ResultSP3 = $Sheet3DataTable | Select-Object -Property $DesiredPropertiesSheet3
#Output to Excel file, CSV file, or to the screen
if ($OutputExcelFilePath) {
if (Test-Path -Path $OutputExcelFilePath) {Remove-Item -Path $OutputExcelFilePath}
$ResultSP1 | Export-Excel -Path $OutputExcelFilePath -WorksheetName "Sheet1InExcel" -AutoSize
$ResultSP2 | Export-Excel -Path $OutputExcelFilePath -WorksheetName "Sheet2InExcel" -AutoSize
$ResultSP3 | Export-Excel -Path $OutputExcelFilePath -WorksheetName "Sheet3InExcel" -AutoSize
}
#at the same time, out put data from StoredProc to CSV
if ($OutputCSVFilePath) {
$Output | Export-Csv -Path $OutputCSVFilePath -NoTypeInformation
}
}
Catch {
$ErrorActionPreference = 'Continue'
$PSCmdlet.WriteError($_)
} }
Hope it helps.