SQL join in powershell

I want to make a SQL join in a powershell something like this

$dataSource = "XXX\SQLEXPRESS"
$user = "XXX"
$pwd = "XXX"
$database = "Test BV"
 
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
 
$query = select mwOrders.fldOrderID, dbo.tblRelatie.fldNaam, dbo.tblRelatie.fldAdres
from .dbo.mwOrders
inner join dbo.tblRelatie
on dbo.tblRelatie.fldRelatieID = mwOrders.fldRelatieID

Thx Danny
LVL 6
Danny KonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,

this should work:
$dataSource = "XXX\SQLEXPRESS"
$user = "XXX"
$pwd = "XXX"
$database = "Test BV"
 
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$connection.Open()
$query = "select mwo.fldOrderID, trel.fldNaam, trel.fldAdres from dbo.mwOrders mwo inner join dbo.tblRelatie trel on trel.fldRelatieID = mwo.fldRelatieID"

$command = $connection.CreateCommand()
$command.CommandText = $query

$result = $command.ExecuteReader()

Open in new window


(adapted from http://irisclasson.com/2013/10/16/how-do-i-query-a-sql-server-db-using-powershell-and-how-do-i-filter-format-and-output-to-a-file-stupid-question-251-255/)

HTH
Rainer
Danny KonAuthor Commented:
Rainer,

I am not good at this, i get the following error:
+ ... ecurity=False;"$connection = New-Object System.Data.SqlClient.SqlConnection
+                    ~~~~~~~~~~~
Unexpected token '$connection' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
Rainer JeschorCommented:
Hi,
sorry but seems one line feed was lost during the code copy:
$dataSource = "XXX\SQLEXPRESS"
$user = "XXX"
$pwd = "XXX"
$database = "Test BV"
 
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$connection.Open()
$query = "select mwo.fldOrderID, trel.fldNaam, trel.fldAdres from dbo.mwOrders mwo inner join dbo.tblRelatie trel on trel.fldRelatieID = mwo.fldRelatieID"

$command = $connection.CreateCommand()
$command.CommandText = $query

$result = $command.ExecuteReader()
$result

Open in new window


The main question now is: what do you want to do with the result?

Please have a look on the link I provided with the first answer - there they use the .NET datatable ...

HTH
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Danny KonAuthor Commented:
Rainer,

Perfect exactly what i needed the rest of the script is also working

Danny
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.