You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PowerShell筛选名称包含PickList的SQL Server表?

解决SQL Server PowerShell模块中模糊筛选表名的问题

看起来你遇到的问题是PowerShell的-like通配符在SQL Server模块的表对象上不生效,这大概率是因为-like对这类特殊对象属性的字符串匹配逻辑有点“挑剔”。试试下面这几种方法来解决:

方法1:用-match正则表达式替代-like

-match会自动将对象属性转换为字符串进行正则匹配,比-like更可靠,只要表名包含PickList就能匹配到:

clear-host
Set-location SQLserver:\sql\...-CONRAD...\default\databases\PT2\tables
$a = Get-ChildItem | Where {$_.Name -match 'PickList'}
foreach ($item in $a) {Write-Host $item}

方法2:直接使用Get-ChildItem的-Filter参数(更高效)

这个方法是在服务器端直接筛选,比客户端用Where-Object过滤更快,尤其当数据库表很多的时候:

clear-host
Set-location SQLserver:\sql\...-CONRAD...\default\databases\PT2\tables
$a = Get-ChildItem -Filter "*PickList*"
foreach ($item in $a) {Write-Host $item}

方法3:先确认表名的实际格式

有时候可能是我们对Name属性的内容理解有误,先输出所有表的架构和名称,确认PickList确实在表名里:

clear-host
Set-location SQLserver:\sql\...-CONRAD...\default\databases\PT2\tables
Get-ChildItem | Select-Object Schema, Name | Format-Table -AutoSize

如果需要同时筛选架构(比如只找pt2架构下的表),可以结合条件:

$a = Get-ChildItem | Where {$_.Schema -eq "pt2" -and $_.Name -match 'PickList'}

内容的提问来源于stack exchange,提问作者Conrad S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:23:23