# SQL Results to variables failing

**URL:** <https://forums.powershell.org/t/sql-results-to-variables-failing/17795>\
**Category:** PowerShell Help\
**Created:** [October 20, 2021, 3:28pm UTC](https://forums.powershell.org/t/sql-results-to-variables-failing/17795 "2021-10-20T15:28:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaunnas](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@shaunnas](https://forums.powershell.org/u/shaunnas)\
**Post date:** [October 20, 2021, 3:28pm UTC](https://forums.powershell.org/t/sql-results-to-variables-failing/17795/1 "2021-10-20T15:28:17Z")

</div>

I’m querying a database in the cloud, and using the data from the results to set variables in my caller script.

The variables drive a build process where I dynamically build out Azure resources.

The variable is not setting in the caller script. I’m a total PowerShell newbie so I’m not quite sure what I’m doing wrong. The function does work, and does return data, it just doesn’t seem to make it back to the caller or I’m setting the variable incorrectly in the caller.

Here is the function I’m using to query for the data, and to send the results back to the calling script:

Function Get-SubscriptionValues {

```
[CmdletBinding()]

Param(

    [Parameter(Mandatory=$true)]

    [string] $tenantid,

    [Parameter(Mandatory = $true)]

    [string] $subscriptionid)

Process

    {

         $thequery = "SELECT prefix, suffix, location FROM dbo.tablenamehere WHERE sub_id = '$subscriptionid'"

         $SQLConnection = New-Object System.Data.SqlClient.SqlConnection    

         $SqlConnection.ConnectionString = "Server=tcp:;Initial Catalog=;Persist Security Info=False;User ID=;Password=;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"

         $SqlCmd = New-Object System.Data.SqlClient.SqlCommand    

         $SqlCmd.CommandText = $thequery

         $SqlCmd.Connection = $SqlConnection  

         $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter    

         $SqlAdapter.SelectCommand = $SqlCmd    

         $DataSet = New-Object System.Data.DataSet    

         $SqlAdapter.Fill($DataSet)    

         $SqlConnection.Close()              

         $thedata = $DataSet.Tables[0]

         return $thedata

        }

    }

```

In the caller script I set the variables like so:

$prefixname = ($thedata).prefix

I tried putting a pause in between when I called the function and when I set the variable to see if maybe the cloud was lagging on returning data to the caller, but the pause didn’t seem to have any affect.

---

<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:** [October 21, 2021, 7:45am UTC](https://forums.powershell.org/t/sql-results-to-variables-failing/17795/2 "2021-10-21T07:45:32Z")

</div>

The variable in the calling function should be set to the value returned by the called function:

```auto
# Using dot notation
$prefixName = (Get-SubscriptionValues).prefix

```

or using `Select-Object`

```auto
$prefixName = Get-SubscriptionValues | Select-Object -ExpandProperty prefix

```

The reason you can’t access `$thedata` directly is due to the variable scope. If they were in the same script, it would work. You can read more about Scopes in the help:

> **[about Scopes - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.2&viewFallbackFrom=powershell-7.1)**
>
> Explains the concept of scope in PowerShell and shows how to set and change the scope of elements.

---

<div class="post-metadata">

**Author:** ![shaunnas](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@shaunnas](https://forums.powershell.org/u/shaunnas)\
**Post date:** [October 21, 2021, 2:49pm UTC](https://forums.powershell.org/t/sql-results-to-variables-failing/17795/3 "2021-10-21T14:49:27Z")

</div>

Ah, OK, that makes sense.

So, does that mean I need to change what I’m returning to the caller as well? Because right now I’m returning this: return $thedata. The sql query brings two other values back, in addition to prefix field.

---

<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:** [October 21, 2021, 8:31pm UTC](https://forums.powershell.org/t/sql-results-to-variables-failing/17795/4 "2021-10-21T20:31:44Z")

</div>

There’s no need to modify it to get it working. The code I’ve given you, when run from the calling function will give you the result you need as it is.

You may consider querying for, and only returning, the prefix if you never use the other data. That might sense from an efficiency point of view, but it’s your call.

---

<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:20pm UTC](https://forums.powershell.org/t/sql-results-to-variables-failing/17795/5 "2024-05-16T20:20:37Z")

</div>

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


