Skip to content

Read-Data

DBSystems: Reads data from a SQL Server table or view

#Requires -Version 5.0
#Requires -Modules SQLServer

[CmdLetBinding(DefaultParameterSetName = "Table")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "Table")]   
    [Parameter(Mandatory = $true, ParameterSetName = "View")]   
    [string]$ServerInstance,    

    [Parameter(Mandatory = $true, ParameterSetName = "Table")]   
    [Parameter(Mandatory = $true, ParameterSetName = "View")]   
    [string]$DatabaseName,

    [Parameter(Mandatory = $true, ParameterSetName = "Table")]       
    [string]$TableName,

    [Parameter(Mandatory = $true, ParameterSetName = "View")]   
    [string]$ViewName,

    [Parameter(ParameterSetName = "Table")]   
    [Parameter(ParameterSetName = "View")] 
    [pscredential]$ServerCredential,

    [Parameter(ParameterSetName = "Table")]   
    [Parameter(ParameterSetName = "View")] 
    [string]$SchemaName = "dbo",

    [Parameter(ParameterSetName = "Table")]   
    [Parameter(ParameterSetName = "View")] 
    [string]$ColumnNames,

    [Parameter(ParameterSetName = "Table")]   
    [Parameter(ParameterSetName = "View")] 
    [string]$ColumnOrder,

    [Parameter(ParameterSetName = "Table")]   
    [Parameter(ParameterSetName = "View")] 
    [Int64]$NumberOfRows,

    [Parameter(ParameterSetName = "Table")]   
    [Parameter(ParameterSetName = "View")] 
    [ValidateSet('ASC','DESC')]
    [string]$ColumnOrderType = "ASC",

    [Parameter(ParameterSetName = "Table")]   
    [Parameter(ParameterSetName = "View")] 
    [int]$ConnectionTimeout = 30
)

Import-Module SQLServer

try {
    [hashtable]$cmdArgs = @{
        'ErrorAction' = 'Stop'
        'ServerInstance' = $ServerInstance
        'ConnectionTimeout' = $ConnectionTimeout
        'DatabaseName' = $DatabaseName
        'SchemaName' = $SchemaName
        'ColumnOrderType' = $ColumnOrderType
    }     
    
    if (-not [string]::IsNullOrWhiteSpace($ColumnNames)) {
        $cmdArgs.Add('ColumnName', $ColumnNames.Split(',').Trim())
    } 
    if (-not [string]::IsNullOrWhiteSpace($ColumnOrder)) {
        $cmdArgs.Add('ColumnOrder', $ColumnOrder.Split(',').Trim())
    }      
    if ($NumberOfRows -gt 0) {
        $cmdArgs.Add('TopN', $NumberOfRows)
    } 
    if ($null -ne $ServerCredential) {
        $cmdArgs.Add('Credential', $ServerCredential)
    }            

    if ($PSCmdlet.ParameterSetName -eq 'Table') {
        $cmdArgs.Add('TableName', $TableName)
        $result = Read-SqlTableData @cmdArgs
    } else {
        $cmdArgs.Add('ViewName', $ViewName)
        $result = Read-SqlViewData @cmdArgs
    }
    
    Write-Output $result
} catch {
    throw
}

Specifies the name of the target computer including the instance name, e.g. MyServer\Instance

Specifies the name of the database that contains the table or view

Specifies the name of the table to read (ParameterSetName: Table)

Specifies the name of the view to read (ParameterSetName: View)

Specifies a PSCredential object for the connection to the SQL Server. ServerCredential is ONLY used for SQL Logins. When you are using Windows Authentication you don't specify -Credential. It is picked up from your current login.

Specifies the name of the schema (defaults to 'dbo')

Specifies the names of columns to return, comma separated

Specifies the names of columns by which to sort the results, comma separated

Specifies the sort order (ASC or DESC)

Specifies the time period to retry the command on the target server

An interactive directory of PowerShell scripts.