Last active
February 5, 2025 04:25
-
-
Save noobow34/57eb32e4de0830c9f2ba0d7cae3fe12a to your computer and use it in GitHub Desktop.
SQL*Plusの実行結果をXMLにしてPowerShellで扱うサンプルスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 文字化け防止 | |
chcp 65001 | Out-Null | |
$Env:NLS_LANG = "Japanese_Japan.UTF8" | |
[console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
# 取得対象のSQL | |
$innerSql = @" | |
SELECT LEVEL SEQ,LEVEL+1 SEQ2 | |
FROM DUAL | |
CONNECT BY LEVEL <= 10 | |
"@ | |
# SQL*Plusで実行するSQL全体 | |
$execSql = @" | |
set pages 0 | |
set long 2000000000 | |
set trimspool on | |
select dbms_xmlgen.getxml('$innerSql') from dual; | |
"@ | |
# SQL*Plusを実行して結果をXMLとして変数に格納 | |
$xml = [xml]($execSql | sqlplus -S user/psss@server/service) | |
"-----------行を指定して使用する-----------" | |
$xml.ROWSET.ROW[1] | |
"-----------行と列を指定して使用する-----------" | |
$xml.ROWSET.ROW[1].SEQ2 | |
"-----------ループで処理する場合-----------" | |
foreach($row in $xml.ROWSET.ROW){ | |
$row.SEQ + ":" + $row.SEQ2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment