PoshCode Archive  Artifact [28ee3985e3]

Artifact 28ee3985e36cebcb737548362902a82f5ebe16bf662f147b62a60aafcd521c0d:

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

# encoding: ascii
# api: powershell
# title: SQL-Update
# description: Function uses the Microsoft SQL cmdlets ‘Invoke-SQLcmd’ to connect to a SQL database and run an UPDATE statement.
# version: 0.1
# type: script
# author: Vidrine
# license: CC0
# function: SQL-Update
# x-poshcode-id: 3337
# x-archived: 2017-04-05T08:43:51
# x-published: 2013-04-10T11:25:00
#
# The target record that will be updated is queried based on a table/column named ‘ID’. Simply change this to query based on another value.
#
<#
.SYNOPSIS
  Author:...Vidrine
  Date:.....2012/04/08
.DESCRIPTION
  Function uses the Microsoft SQL cmdlets 'Invoke-SQLcmd' to connect to a SQL database and run an UPDATE statement.
  The target record that will be updated is queried based on a table/column named 'ID'. Simply change this to query based on another value.
.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 dataField
  Field/Column name from the specified table. This is the field/column that will be updated.
.PARAM dataValue
  The new value of the field/column that will be updated.
.PARAM updateID
  The ID of the target record to update.
.EXAMPLE
SQL-Update -server $sqlServerInstance -database $sqlDatabase -table $sqlTable -dataField $sqlDataField -dataValue $sqlDataValue -updateID $sqlDataID
#>
function SQL-Update {
param(
	[string]$server,
	[string]$database,
	[string]$table,
	[string]$dataField,
	[string]$dataValue,
	[string]$updateID
)

$sqlQuery = @"
UPDATE $database.$table 
SET $dataField='$dataValue' 
WHERE id=$updateID
"@

try {
Invoke-SQLcmd -ServerInstance $server -Database $database -Query $sqlQuery
}
catch {
}
}