Table Class
Assembly: Microsoft.SqlServer.Smo (in microsoft.sqlserver.smo.dll)
Updated text:
Updated text:
This namespace, class, or member is supported only in version 2.0 of the Microsoft .NET Framework.
To get Table object properties, users can be a member of the public fixed server role.
To set Table object properties, users must have ALTER permission on the table.
To create a table, users must have CREATE TABLE permission on the parent database and ALTER permission in the schema.
To drop a table, users must have CONTROL permission on the table, or be a member of the db_ddladmin or db_owner fixed database roles.
To grant, deny, or revoke permission on the table to other users, users must have CONTROL permission on the table.
Microsoft.SqlServer.Management.Smo.SmoObjectBase
Microsoft.SqlServer.Management.Smo.SqlSmoObject
Microsoft.SqlServer.Management.Smo.NamedSmoObject
Microsoft.SqlServer.Management.Smo.ScriptNameObjectBase
Microsoft.SqlServer.Management.Smo.ScriptSchemaObjectBase
Microsoft.SqlServer.Management.Smo.TableViewBase
Microsoft.SqlServer.Management.Smo.Table
Development Platforms
For a list of the supported platforms, see Hardware and Software Requirements for Installing SQL Server 2005.Target Platforms
#-------------------------------------------------------------------------
# ScriptName : get-SQLServerTables.ps1
#
# Author : Richard Vantrease
#
# Purpose : Returns a collection of table objects for each
# database on a SQL Server. Will display interesting
# properties if the -Display switch is used.
#
# Dependencies : Microsoft.SQLServer.SMO
#-------------------------------------------------------------------------
param(
[string]$ServerName,
[switch]$Display
)
#-------------------------Environment Preferences-------------------------
Set-PSDebug -strict
#-------------------------------------------------------------------------
# Load Microsoft.SQLServer.SMO Library
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$SQLServer = new-object "Microsoft.SQLServer.Management.SMO.Server" $ServerName
if($Display)
{
foreach($Database in $SQLServer.Databases)
{
$Database.Tables | ft Name, RowCount, DataSpaceUsed -AutoSize
}
}
else
{
foreach($Database in $SQLServer.Databases){ $Database.Tables }
}
- 6/25/2009
- Richard Vantrease
- 7/8/2009
- Thomas Lee
#-------------------------------------------------------------------------
# ScriptName : create-SQLTable.ps1
#
# Author : Richard Vantrease
#
# Purpose : Create a new SQL Server table
#
# NOTE: I didn't really bother parameterizing the table columns as there
# are too many variables to make it feasible for this example. So, it will
# simply create two columns:
# A VARCHAR(50)
# B INT
#
# Dependencies : Microsoft.SQLServer.SMO
#-------------------------------------------------------------------------
param
(
[string]$ServerName,
[string]$DBName,
[string]$TableName
)
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')
#http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.sqldatatype.aspx
$DataTypes = [Microsoft.SQLServer.Management.SMO.SQLDataType]
$_VarChar = $DataTypes::VarChar
$_INT = $DataTypes::Int
$SQLServer = new-object ('Microsoft.SQLServer.Management.SMO.Server') $ServerName
$Database = $SQLServer.Databases[$DBName]
$Table = new-object 'Microsoft.SQLServer.Management.SMO.Table' ($Database, $TableName)
$Column = new-object 'Microsoft.SQLServer.Management.SMO.Column' ($Table, 'A', $_VarChar)
$Column.Properties['Length'].Value = 50
$Table.Columns.Add($Column)
$Column = new-object 'Microsoft.SQLServer.Management.SMO.Column' ($Table, 'B', $_INT)
$Table.Columns.Add($Column)
$Database.Tables.Add($Table)
$Database.Alter()
$SQLServer.Alter()
$Table.Create()
- 6/25/2009
- Richard Vantrease
- 7/8/2009
- Thomas Lee