# Import CSV file to global temp table in SQL

**URL:** <https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630>\
**Category:** PowerShell Help\
**Created:** [September 24, 2021, 11:41pm UTC](https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630 "2021-09-24T23:41:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ollyjolly](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@ollyjolly](https://forums.powershell.org/u/ollyjolly)\
**Post date:** [September 24, 2021, 11:41pm UTC](https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630/1 "2021-09-24T23:41:57Z")

</div>

I am attempting to use dbatools to allow me to import a CSV file into a global temp table under tempdb on SQL Server.

Below is the powershell script I am using. When I run the script it prompts for the path for the CSV file, file delimiter, DB Instance. After entering those items the CSV data is imported to $data, and the Write-DbaDataTable executes and I am prompted for the Table name.

After entering the global temp table name I see the global temp table in SQL under tempdb, and then the insert process occurs. Once the insert process is complete the table disappears from SQL.

I am not sure what I need to change in the script to get the global temp table to stay in SQL.

```auto
$File = Read-Host -Prompt "Enter Path to File"
if ($File )
	{
    Write-Host "File: [$File]"
	}
	else {
        Read-Host -Prompt "Enter Path to File"
	     }

$Delimiter = Read-Host -Prompt "Enter Delimiter"
if ($File )
	{
    Write-Host "Delimiter: [$Delimiter]"
	}
	else {
        Read-Host -Prompt "Enter Path to File"
	     }
		
$Instance = Read-Host -Prompt "Enter Db instance"
if ($Instance )
	{
    Write-Host "Instance: [$Instance]"
	}
	else {
        Read-Host -Prompt "Enter Db instance"
	     }

$Database = "tempdb"
$data = Import-Csv -Path $File -Delimiter $Delimiter | ConvertTo-DbaDataTable
for ($i = 1; $i -le 100; $i++ )
{
    Write-Progress -Activity "Search in Progress" -Status "$i% Complete:" -PercentComplete $i
    Start-Sleep -milliseconds 250
}

Write-DbaDataTable -SqlInstance $Instance -Database $Database -InputObject $data -AutoCreateTable
for ($i = 1; $i -le 100; $i++ )
{
    Write-Progress -Activity "Search in Progress" -Status "$i% Complete:" -PercentComplete $i
    Start-Sleep -milliseconds 250
}

```

It is almost like SQL isn’t keeping the instance of the global temp table. Can an imports be done to a global temporary table in a SQL database where you can then access the global temp table in SQL after the Powershell script is done? What am I missing?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 26, 2021, 8:39pm UTC](https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630/2 "2021-09-26T20:39:03Z")

</div>

You will need to find a way to persist the connection until you’re done doing what you want to do. If no one else uses the table, and you disconnect, the table will be deleted.:

_If you create the global temporary table **##employees** , any user in the database can work with this table. If no other user works with this table after you create it, the table is deleted when you disconnect. If another user works with the table after you create it, SQL Server deletes it after you disconnect and after all other sessions are no longer actively using it._

> **[Creating and Modifying Table Basics](https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms177399(v=sql.105)#temporary-tables)**

---

<div class="post-metadata">

**Author:** ![ollyjolly](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@ollyjolly](https://forums.powershell.org/u/ollyjolly)\
**Post date:** [September 27, 2021, 1:24am UTC](https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630/3 "2021-09-27T01:24:25Z")

</div>

Thanks Matt! I was afraid of that. Is it possible to make the connection persist from within powershell?

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 27, 2021, 7:50am UTC](https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630/4 "2021-09-27T07:50:21Z")

</div>

I think, from the dbatools examples, you need to use `Connect-DbaInstance`. You assign the connection to a variable, then pass that variable to the `-SqlInstance` parameter of `Write-DbaDataTable`. The connection should persist until you call `Disconnect-DbaInstance`.

---

<div class="post-metadata">

**Author:** ![ollyjolly](https://avatars.discourse-cdn.com/v4/letter/o/858c86/32.png) [@ollyjolly](https://forums.powershell.org/u/ollyjolly)\
**Post date:** [September 27, 2021, 2:44pm UTC](https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630/5 "2021-09-27T14:44:01Z")

</div>

That worked perfectly! Appreciate the insight.

---

<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:21pm UTC](https://forums.powershell.org/t/import-csv-file-to-global-temp-table-in-sql/17630/6 "2024-05-16T20:21:17Z")

</div>

Closed on May 16, 2024, 8:21pm UTC


