8

I am wondering if there is any way to use values for a dataset in a subsequent query.
Here is an example:

Works:

$Result = Invoke-Sqlcmd -query "SELECT Id, FirstName, LastName FROM Person" 
foreach($item in $Result){
  $ID= $item.Id 
  $SaleResults = Invoke-Sqlcmd -query "SELECT * FROM Sales WHERE Salesperson = $ID"
}

Does NOT Work:

foreach($item in $Result){
  $SaleResults = Invoke-Sqlcmd -query "SELECT * FROM Sales WHERE Salesperson=$Item.Id"
}

The second example fails because it is using $Item.Id as a string rather that uniqueidentifier, hence the error: "Conversion failed when converting from a character string to uniqueidentifier."

Any idea how to use these values with their correct type, without creating a separate variable?

1 Answer 1

9

You need to use $($Item.ID) in your double-quoted string, and it will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.