如何仅获取字段名称的值?

How do I get just the value of the field name?

提问人:Rod 提问时间:11/9/2023 最后编辑:Thom ARod 更新时间:11/9/2023 访问量:70

问:

我正在尝试使用 sqlcmd 实用程序并且查询正在工作,我只是想知道如何在没有字段名称和那些破折号的情况下获取值?

$res = sqlcmd -Q @"
    :Connect . 
    use [Database1]

    select firstname from customer
"@

$res

输出:

firstname                                         
--------------------------------------------------
John

enter image description here

PowerShell SQLCMD

评论

1赞 Thom A 11/9/2023
提问时,请不要上传代码/数据/错误的图片。
2赞 mklement0 11/9/2023
顺便说一句:PowerShell ISE 不再积极开发,并且有理由不使用它(底部),特别是无法运行 PowerShell (Core) 7+。积极开发的跨平台编辑器提供最佳 PowerShell 开发体验的是 Visual Studio Code 及其 PowerShell 扩展

答:

1赞 Nick Abbot 11/9/2023 #1

您需要 ,并尝试以下操作:-h -1-m 1set nocount on;

$res = sqlcmd -S . -m 1 -h -1 -Q @"
    use [Database1];
    set nocount on;
    select firstname from customer;
"@

评论

0赞 Rod 11/9/2023
如果不是的话,在钱上真的很接近,但是,我得到了额外的元Sqlcmd: Successfully connected to server '.'.
0赞 Nick Abbot 11/9/2023
服务器是本地的,请尝试省略该行。您可能不需要它。:Connect
0赞 Nick Abbot 11/9/2023
请尝试以下操作:sqlcmd -S . -m 1 -h -1 -Q @"
0赞 Nick Abbot 11/9/2023
您可以使用如下方式指定远程系统:如果连接受信任,则使用,否则使用用户名和密码。-Ssqlcmd -S SQLServer/Instance -E -m 0 -h -1 -Q "set nocount on;select getdate()"-E
0赞 mklement0 11/9/2023
没关系我的评论(已删除);我看到一个不存在的;它与 一起存在,但与 不同,文档没有提到需要直接将 附加到选项名称。-m -1-1-h-m-1
2赞 lit 11/9/2023 #2

PowerShell 是关于使用类型的自动化。 返回到解析文本字符串。 不知道 Shinola 的类型。sqlcmd.exesqlcmd.exe

用。它甚至会将值作为正确的类型返回,而不仅仅是字符串。Invoke-Sqlcmd

(Invoke-Sqlcmd -ServerInstance DEVDW02 -Query "SELECT firstname FROM customer;").firstname

((Invoke-Sqlcmd -ServerInstance DEVDW02 -Query "SELECT firstname FROM customer;").firstname).GetType()