How to get table columns with datatypes (and lengths) using PowerShell?
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:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType
$colNames
How to get also datatype lengths for columns?
sql-server sql-server-2008 powershell
migrated from stackoverflow.com Oct 3 '11 at 11:36
This question came from our site for professional and enthusiast programmers.
add a comment |
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:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType
$colNames
How to get also datatype lengths for columns?
sql-server sql-server-2008 powershell
migrated from stackoverflow.com Oct 3 '11 at 11:36
This question came from our site for professional and enthusiast programmers.
add a comment |
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:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType
$colNames
How to get also datatype lengths for columns?
sql-server sql-server-2008 powershell
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:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType
$colNames
How to get also datatype lengths for columns?
sql-server sql-server-2008 powershell
sql-server sql-server-2008 powershell
asked Sep 30 '11 at 13:04
jrarajrara
2,363174862
2,363174862
migrated from stackoverflow.com Oct 3 '11 at 11:36
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Oct 3 '11 at 11:36
This question came from our site for professional and enthusiast programmers.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The lengths are found at <Column>.Properties['Length'].Value
, so you can select it like:
#Get column names
$colNames = dir 'SQLSERVER:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType, `
@{Name='Length'; Expression = {$_.Properties['Length'].Value}}
$colNames
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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'
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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)
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f6437%2fhow-to-get-table-columns-with-datatypes-and-lengths-using-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The lengths are found at <Column>.Properties['Length'].Value
, so you can select it like:
#Get column names
$colNames = dir 'SQLSERVER:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType, `
@{Name='Length'; Expression = {$_.Properties['Length'].Value}}
$colNames
add a comment |
The lengths are found at <Column>.Properties['Length'].Value
, so you can select it like:
#Get column names
$colNames = dir 'SQLSERVER:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType, `
@{Name='Length'; Expression = {$_.Properties['Length'].Value}}
$colNames
add a comment |
The lengths are found at <Column>.Properties['Length'].Value
, so you can select it like:
#Get column names
$colNames = dir 'SQLSERVER:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType, `
@{Name='Length'; Expression = {$_.Properties['Length'].Value}}
$colNames
The lengths are found at <Column>.Properties['Length'].Value
, so you can select it like:
#Get column names
$colNames = dir 'SQLSERVER:SQLMYCOMPUTERMYSQLINSTANCEDatabasesMYDATABASETables' |
Where-Object {$_.DisplayName -match "dbo.MYTABLE"} |
ForEach-Object {$_.Columns} |
Select-Object Name, DataType, `
@{Name='Length'; Expression = {$_.Properties['Length'].Value}}
$colNames
answered Sep 30 '11 at 13:37
RynantRynant
16613
16613
add a comment |
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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'
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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'
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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'
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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'
edited 3 mins ago
John Baughman
1051
1051
answered Sep 30 '11 at 13:18
DirtyPaws
add a comment |
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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)
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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)
add a comment |
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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)
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, -- Type is an int in the columns table, this returns the type name.
MaxLength = c.max_length -- Returns the max length of the column.
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)
edited 3 mins ago
John Baughman
1051
1051
answered Jan 28 '16 at 11:11
Jonathan H.Jonathan H.
1112
1112
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f6437%2fhow-to-get-table-columns-with-datatypes-and-lengths-using-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown