PoshCode Archive  Artifact Content

Artifact 39ed5ba8dff4f949c915070757f886f1dcc47782c72ef1ad16839027cb5d89b2:

  • File SQL-Select.ps1 — part of check-in [a24916eae7] at 2018-06-10 13:24:07 on branch trunk — Function uses the Microsoft SQL cmdlets ‘Invoke-SQLcmd’ to connect to a SQL database and run a SELECT statement. (user: Vidrine size: 2017)

# encoding: ascii
# api: powershell
# title: SQL-Select
# description: Function uses the Microsoft SQL cmdlets ‘Invoke-SQLcmd’ to connect to a SQL database and run a SELECT statement.
# version: 0.1
# type: script
# author: Vidrine
# license: CC0
# function: SQL-Select
# x-poshcode-id: 3338
# x-archived: 2014-08-18T21:32:59
# x-published: 2014-04-10T11:32:00
#
# Results of the query are returned. Store returned results in a variable to be able to interact with them as an object.
#
<#
.SYNOPSIS
  Author:...Vidrine
  Date:.....2012/04/08
.DESCRIPTION
  Function uses the Microsoft SQL cmdlets 'Invoke-SQLcmd' to connect to a SQL database and run a SELECT statement.
  Results of the query are returned. Store returned results in a variable to be able to interact with them as an object.
.PARAM server
  Hostname/IP of the server hosting the SQL database.
.PARAM database
  Name of the SQL database to connect to.
.PARAM table
  Name of the table within the specified SQL database.
.PARAM selectWhat
  [String] Select string. This will specify the SELECT value to use in the SQL statement. Default value is '*'.
.PARAM where
  [String] Where string. This will specify the WHERE clause to use in the SQL statement. ie. "id='64'"
.EXAMPLE
$results = SQL-Select -server $sqlServerInstance -database $sqlDatabase -table $sqlTable -selectWhat '*'
.EXAMPLE
$results = SQL-Select -server $sqlServerInstance -database $sqlDatabase -table $sqlTable -selectWhat '*' -where "id='64'"
#>
function SQL-Select {
param(
	[string]$server,
	[string]$database,
	[string]$table,
	[string]$selectWhat = '*',
	[string]$where
	
)

## SELECT statement with a WHERE clause
if ($where){
$sqlQuery = @"
SELECT $selectWhat 
FROM $table 
WHERE $where
"@
}

## General SELECT statement
else {
$sqlQuery = @"
SELECT $selectWhat 
FROM $table
"@
}

try
{
$results = Invoke-SQLcmd -ServerInstance $server -Database $database -Query $sqlQuery

return $results
}
catch{}
}