Update-Table

Adds new columns or alters existing columns on an existing table.

Syntax

Update-Table [-Name] <String> [-SchemaName <String>] [-AddColumn <ScriptBlock>] [-UpdateColumn <ScriptBlock>] [-RemoveColumn <String[]>] [<CommonParameters>]

Description

The Update-Table operation adds, updates, and removes columns from a table. Columns are added, then updated, then removed.

The new columns for the table should be created and returned in a script block, which is passed as the value of the AddColumn parameter. For example,

Update-Table 'Suits' -AddColumn {
    Bit 'HasVest' -NotNull -Default 0
}

The new definitions for existing columns should be created and returned in a script block, which is passed as the value of the UpdateColumn parameter. For example,

Update-Table 'Suits' -UpdateColumn {
    VarChar 'Color' 256 -NotNull
}

Related Commands

Parameters

Name Type Description Required? Pipeline Input Default Value
Name String

The name of the table.

true false
SchemaName String

The table's schema. Defaults to dbo.

false false dbo
AddColumn ScriptBlock

A script block that returns the new columns to add to a table.

false false
UpdateColumn ScriptBlock

A script block that returns new column definitions for existing columns

false false
RemoveColumn String[]

Columns to remove.

false false

EXAMPLE 1

Update-Table -Name 'Ties' -AddColumn { VarChar 'Color' 50 -NotNull }

Adds a new Color column to the Ties table. Pretty!

EXAMPLE 2

Update-Table -Name 'Ties' -UpdateColumn { VarChar 'Color' 100 -NotNull }

Demonstrates how to change the definition of an existing column.

EXAMPLE 3

Update-Table -Name 'Ties' -RemoveColumn 'Pattern','Manufacturer'

Demonstrates how to remove columns from a table.