Powershell and Oracle, exporting SQL data to csv -
i have oracle sql table contains email addresses. need extract data , insert array, , export csv file. i'm not sure go here. code far is:
$odpassemblyname = "oracle.dataaccess, version=2.112.3.0, culture=neutral, publickeytoken=89b483f429c47342" [system.reflection.assembly]::load($odpassemblyname) $con = new-object oracle.dataaccess.client.oracleconnection("user id=hr;password=oracle;data source=xe") $cmd=$con.createcommand() $cmd.commandtext= @" select distinct email hr.emails acct_enabled = 'y' , upper(email) not 'ais%'order email "@ $con.open() $rdr=$cmd.executereader() $columnnames=$rdr.getschematable() | select-object -expandproperty columnname $resultset=@() while ($rdr.read()) { $intdata=$rdr.getoraclestring(0) "{0,0}" -f $intdata.value } $con.close()
can me while loop (i'm new programming mostly) , me add result set array, , export result set nice little csv file?
thanks in advance help/pointers
james
try this, although cant test based on no oracle db available on side.
$odpassemblyname = "oracle.dataaccess, version=2.112.3.0, culture=neutral, publickeytoken=89b483f429c47342" [system.reflection.assembly]::load($odpassemblyname) $con = new-object oracle.dataaccess.client.oracleconnection("user id=hr;password=oracle;data source=xe") $cmd=$con.createcommand() $cmd.commandtext= @" select distinct email hr.emails acct_enabled = 'y' , upper(email) not 'ais%'order email "@ $con.open() $sqladapter = new-object system.data.sqlclient.sqldataadapter $sqladapter.selectcommand = $cmd $dataset = new-object system.data.dataset $sqladapter.fill($dataset) $emaillist = $dataset.tables[0].email $con.close() $emaillist | export-csv "c:\somefolder\oracleemails.csv" -notypeinformation
Comments
Post a Comment