# SQL table header names export

**URL:** https://forums.powershell.org/t/sql-table-header-names-export/8964
**Category:** PowerShell Help
**Created:** [July 6, 2017, 10:42am UTC](https://forums.powershell.org/t/sql-table-header-names-export/8964 "2017-07-06T10:42:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![voytek-szaro](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@voytek-szaro](https://forums.powershell.org/u/voytek-szaro)
#### Post date: [July 6, 2017, 10:42am UTC](https://forums.powershell.org/t/sql-table-header-names-export/8964/1 "2017-07-06T10:42:41Z")

</div>

Hi all, I’m trying to get output of this little script into a variable in powershell, however, I can’t figure out how to do it, I just need to query column names and add them to an array, that’s it, no need for any other data… looks pretty simple, eh?

```
$DBServer = "server\instance"
$DBName = "dbname"
$Uid="sa"
$Pwd="Password"
$SQLCon = New-Object System.Data.SqlClient.SqlConnection("Data Source=$DBServer; `
		  Initial Catalog=$DBName;Integrated Security=False;User ID=$Uid;Password=$Pwd")
	$SQLCon.open()
        $SQL = $SQLCon.CreateCommand()

$query = 
@"
            declare @Result varchar(max)='
            '
            select @Result=@Result+''+ColumnName+'
            '
            from
            (
                select
                    replace(col.name, ' ', '_') ColumnName,
                    column_id ColumnId
                from sys.columns col
                    join sys.types typ on
                        col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
                where object_id = object_id('dbo.sometable')
            ) t
            order by ColumnName;
            print @Result
"@
$SQL.CommandText = $query
$Result1 = $SQL.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($Result1)
Write-Host ($table | Format-Table | Out-String)
$SQLCon.Close()
```

Thank you

---

<div class="post-metadata">

### Author: ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)
#### Post date: [July 6, 2017, 2:35pm UTC](https://forums.powershell.org/t/sql-table-header-names-export/8964/2 "2017-07-06T14:35:55Z")

</div>

Don’t have a SQL server to test right now but I guess the first questions are.

1. Does the query produce the correct output in e.g. SQL Management Studio?
2. What is the result you’re getting.

---

<div class="post-metadata">

### Author: ![voytek-szaro](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@voytek-szaro](https://forums.powershell.org/u/voytek-szaro)
#### Post date: [July 7, 2017, 1:43pm UTC](https://forums.powershell.org/t/sql-table-header-names-export/8964/3 "2017-07-07T13:43:13Z")

</div>

1. yes i do get a correct result in the management studio
2. I’m getting just the header names.  
THanks!

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:37pm UTC](https://forums.powershell.org/t/sql-table-header-names-export/8964/4 "2024-05-16T20:37:55Z")

</div>


