4

I have several queries that I use for identifying issues in a SQL database but I'm trying to create at powershell script that I can use to do this automatically. The trouble I am having is that when I invoke my SQL scripts there are multiple result sets and my script only seems to capture the first set. I'm wondering what I need to do to cycle through all the results. This is the code with just some simple selects

$dataSource = 'Server'
$database = "DB"
$sqlcommand = @"
Select TOP 1000 * from tblA;
Select TOP 1000 * from tblB
"@


Function Convert-Dataset
{
    Param
    (
        [Parameter(Mandatory=$true)]
        $dataset
    )

    Begin
    {
        $return=@()
        For($r = 0; $r -lt $dataset.tables[0].rows.Count; $r++)
        {
            $table= new-object psobject
            If($dataset.tables[0].columns.ColumnName.Count -le 1)
            {
                $colName = [String]$dataset.tables[0].columns.ColumnName
                If($dataset.tables[0].rows.Count -eq 1)
                {
                    $colValue = [string]$dataset.tables[0].rows.$colName
                }
                Else
                {
                    $colValue = [string]$dataset.tables[0].rows[$r].$colName
                }
                $table | Add-Member -memberType noteproperty -Name $colName -Value $colValue
            }
            Else{
                For($c = 0; $c -lt $dataset.tables[0].columns.ColumnName.Count; $c++)
                {
                    $colName = [String]$dataset.tables[0].columns.ColumnName[$c]
                    $colValue = [string]$dataset.tables[0].rows[$r][$c]
                    $table | Add-Member -memberType noteproperty -Name $colName -Value $colValue
                }
            }
            $return +=$table
        }
    }
    End
    {
        Return $return
    }
}

$connectionString = "Data Source=$dataSource; " +
        "Integrated Security=True; " +
        "Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$return=Convert-Dataset -dataset $dataset
$return | Out-GridView
2
  • Where does "convert-dataset" come from? Commented Jul 13, 2017 at 19:08
  • I wrote convert-dataset as a way to be able to handle content returned using $result.ColumnName Commented Jul 13, 2017 at 19:47

1 Answer 1

7

I figured it out

$connectionString = "Data Source=$dataSource; " +
        "Integrated Security=True; " +
        "Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()

ForEach($table in $dataset.Tables)
{
        $table |Out-GridView -PassThru
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.