Powershell-从SQLPlus查询中检索计数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Powershell-从SQLPlus查询中检索计数相关的知识,希望对你有一定的参考价值。

我正在使用Powershell脚本,该脚本利用SQLPlus查询oracle数据库。

查询是:select count(*) from table

Powershell查询输出:

SQL*Plus: Release 11.2.0.1.0 Production on Sat Feb 6 09:50:52 2016

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options


  COUNT(*)
----------
    50

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

我目前正在使用下面的语句来检索Count,尽管它似乎没有捕获结果。

if($file -match 'count (?<count>d+)'){$count=[int32]$matches['count']}

任何帮助/指导将不胜感激。谢谢!

答案

我看到一个问题和一个潜在的问题。我怀疑后者$file是一个字符串数组。这对您不利,因为您要根据上面几行文字的存在来查找数字。

问题是您的正则表达式不是多行的,并且在示例中,数字与计数不在同一行。您的正则表达式仅匹配Count 50。那不是它在您显示的文本中出现的方式。

[确保两个点都让$file为一个字符串,然后运行与“ COUNT”之后遇到的第一个数字匹配的多行正则表达式。保留您拥有的命名匹配逻辑,因为这很高兴。

# This can most likely be moved to where this is read in.
$file = $file | Out-String

# try and match the regex. 
if($file -match "(?s)Count.*?(?<count>d+)"){
    [int32]$Matches.Count
}

以上是关于Powershell-从SQLPlus查询中检索计数的主要内容,如果未能解决你的问题,请参考以下文章

如何仅将 SQLPlus 错误保存到文件(从 powershell 调用)

尝试从 powershell 命令行使用 sqlplus 执行 sql 脚本

如何使用 Powershell 进行复杂查询 MongoDB

如何使用 powershell 中的 sqlplus 运行 .sql 文件

CakePHP 3:如何在一次查询中统计和检索不同时期的数据?

查询结果设置为变量 - powershell