Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get column names with data types (and lengths) for SQL Server instance using PowerShell. I got this far:

#Load PSSnapin
Add-PSSnapin *SQL*

#Get column names
$colNames = dir 'SQLSERVER:\SQL\MYCOMPUTER\MYSQLINSTANCE\Databases\MYDATABASE\Tables' | 
    Where-Object {$_.DisplayName -match "dbo.MYTABLE"} | 
        ForEach-Object {$_.Columns} |
            Select-Object Name, DataType
$colNames

How to get also datatype lengths for columns?

share|improve this question

migrated from stackoverflow.com Oct 3 '11 at 11:36

This question came from our site for professional and enthusiast programmers.

up vote 5 down vote accepted

The lengths are found at <Column>.Properties['Length'].Value, so you can select it like:

#Get column names
$colNames = dir 'SQLSERVER:\SQL\MYCOMPUTER\MYSQLINSTANCE\Databases\MYDATABASE\Tables' | 
    Where-Object {$_.DisplayName -match "dbo.MYTABLE"} | 
        ForEach-Object {$_.Columns} |
            Select-Object Name, DataType, `
                @{Name='Length'; Expression = {$_.Properties['Length'].Value}}
$colNames
share|improve this answer

Unfortunately, I don't know the PowerShell syntax, but, heres the SQL for what you want:

SELECT 
    TableName = OBJECT_NAME(c.OBJECT_ID), 
    ColumnName = c.name,
    DataType = t.name,
    MaxLength = t.max_length
FROM 
    sys.columns AS c
JOIN 
    sys.types AS t 
ON c.user_type_id=t.user_type_id
WHERE 
    OBJECT_NAME(c.OBJECT_ID) = 'MYTABLE'
share|improve this answer

Thanks to DirtyPaws. SQL Statement with Powershell Syntax:

#Credentials
$SQLServer = "SQL ServerName/IP Address"
$SQLDBName = "DatabaseName"
$uid ="MySqlUser"
$pwd = "MySqlUserPassword"

#Establish SQL Connection
$connectionString = "Server=$SQLServer;Database = $SQLDBName; User ID = $uid; Password = $pwd;"

$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()

#Create SQL Statement
$query = "
SELECT 
    TableName = OBJECT_NAME(c.OBJECT_ID), 
    ColumnName = c.name,
    DataType = t.name,
    MaxLength = t.max_length
FROM 
    sys.columns AS c
JOIN 
    sys.types AS t 
ON c.user_type_id=t.user_type_id
WHERE 
    OBJECT_NAME(c.OBJECT_ID) = 'MyTableName'
"

#Add SQL Query to SQL Connection
$command = $connection.CreateCommand()
$command.CommandText = $query

#Execute SQL Query
$result = $command.ExecuteReader()

#Add result to DataTable Object and Display it
$table = new-object “System.Data.DataTable”
$table.Load($result)

Write-Host ($table | Format-Table | Out-String)
Write-Host ($table | Format-List | Out-String)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.