Functions/GenXdev.Data.SqlServer/Get-SQLServerViewData.ps1
|
############################################################################### <# .SYNOPSIS Retrieves data from a SQL database view with optional record limiting. .DESCRIPTION Queries a SQL database view using either a connection string or database file path. The function supports limiting the number of returned records and provides verbose output for tracking query execution. .LICENSE Copyright (C) 2026 René Vaessen / GenXdev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. .PARAMETER ConnectionString The connection string to connect to the SQL database. This parameter is mutually exclusive with DatabaseFilePath. .PARAMETER DatabaseFilePath The file path to the SQL database file. This parameter is mutually exclusive with ConnectionString. .PARAMETER ViewName The name of the view from which to retrieve data. .PARAMETER Count The maximum number of records to return. Use -1 to return all records. Defaults to 100 if not specified. .EXAMPLE Get-SQLServerViewData -DatabaseFilePath "C:\MyDb.sqlite" ` -ViewName "CustomerView" ` -Count 50 .EXAMPLE Get-SQLServerViewData "C:\MyDb.sqlite" "CustomerView" #> function Get-SQLServerViewData { [CmdletBinding(DefaultParameterSetName = 'DatabaseName')] param ( ######################################################################## [Parameter( Position = 0, Mandatory = $true, ParameterSetName = 'ConnectionString', HelpMessage = 'The connection string to the SQL Server database.' )] [ValidateNotNullOrEmpty()] [string]$ConnectionString, ######################################################################## [Parameter( Position = 0, Mandatory = $true, ParameterSetName = 'DatabaseName', HelpMessage = 'The name of the SQL Server database.' )] [ValidateNotNullOrEmpty()] [string]$DatabaseName, ######################################################################## [Parameter( Position = 1, ParameterSetName = 'DatabaseName', HelpMessage = 'The SQL Server instance name.' )] [string]$Server = 'localhost', ######################################################################## [Parameter( Position = 2, Mandatory = $true, HelpMessage = 'The name of the view to query.' )] [ValidateNotNullOrEmpty()] [string]$ViewName, ######################################################################## [Parameter( Position = 3, Mandatory = $false, HelpMessage = 'Number of records to return. -1 for all records.' )] [ValidateRange(-1, [int]::MaxValue)] [int]$Count = 100 ) begin { Microsoft.PowerShell.Utility\Write-Verbose "Starting Get-SQLServerViewData for view: $ViewName" } process { # construct query with optional record limit $query = if ($Count -eq -1) { "SELECT * FROM $ViewName" } else { "SELECT * FROM $ViewName LIMIT $Count" } Microsoft.PowerShell.Utility\Write-Verbose "Executing query: $query" # prepare parameters for Invoke-SQLServerQuery $PSBoundParameters['Queries'] = $query # execute query and return results GenXdev\Invoke-SQLServerQuery @PSBoundParameters } end { Microsoft.PowerShell.Utility\Write-Verbose "Completed querying view: $ViewName" } } |