0

I have a problem with powershell and oracle.

This is my code

Add-Type -Path "C:\app\aasif\product\11.2.0\client_2\odp.net\managed\common\Oracle.ManagedDataAccess.dll"

$con = New-Object Oracle.ManagedDataAccess.Client.OracleConnection("User Id=sys;Password=password;Data Source=myserver/oracle;DBA privilege=SYSDBA")

$con.Open()
$cmd=$con.CreateCommand()
$cmd.CommandText="select distinct owner from all_tables where table_name = 'mytable'"

$rdr=$cmd.ExecuteReader()

if ($rdr.Read()) {
  $rdr.GetString(0)
}

$con.Close()

When i execute this query directly with SQLPlus, i have :

RS123
RS456
RS789
RS741
RS963

With my powershell, i can't view all the data returned by the query, but only the first line.

RS123

How can i do this?

Thanks

2 Answers 2

2

I think in place of if($rdr.Read()) you have to write while($rdr.Read()) to read and work with all the value from the output of the query.

Actually I am very new in powershell but in general language we have to put this condition in loop.

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

Comments

1

CreateCommand() returns an OracleCommand. ExecuteReader() returns and OracleDataReader.

Here's and example of using the OracleDataReader class:

$OracleDataReader = $cmd.ExecuteReader()
while ($OracleDataReader.Read()) {
    $OracleDataReader.GetString(0)
}

Notice I used GetString. You'll need to use the data reader method appropriate for the data type of the column corresponding to your SQL query. The available methods are listed here. If you don't know what the data type is you can use GetFieldType() where the parameter is the column ID you want to check.

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.