【问题标题】:How do i copy the output of a powershell script (with multiple return values) into postgresql table?如何将 powershell 脚本的输出(具有多个返回值)复制到 postgresql 表中?
【发布时间】:2021-02-17 15:27:40
【问题描述】:

我有一个返回 3 个值的小型 powershell 脚本:

test.ps1

返回“2020-01-01”、“test1”、“test2”

我想用程序命令的复制将这 3 个值复制到 postgresql 表中:

从程序中复制 mytable 'powershell -executionpolicy remotesigned -File test.ps1'

问题是 powershell 似乎在三行中输出值:

2020-02-01

测试1

测试2

复制命令给了我错误:

"列 col1 缺少数据" SQL 状态:22007

(很抱歉没有包含完整的消息,但不是英文)

我该如何解决?

我试过了:

test.ps1

return '2020-02-01, test1, test2'

但是这只会返回一个值并且失败并显示相同的消息。

我想我可以将 powershell 脚本的输出写入文本文件并从那里复制,但这似乎不是一个好的解决方案,因为我仍然需要以某种方式从 sql 脚本调用脚本。

谢谢

编辑:mytable 有 3 个三列:(date, col1, col2)

【问题讨论】:

    标签: postgresql powershell


    【解决方案1】:

    好的,找到了解决方案:

    test.ps1

    return '2020-02-01, test1, test2'

    sql:

    复制 mytable FROM PROGRAM 'powershell -executionpolicy remotesigned -File test.ps1' DELIMITER ',' CSV;

    【讨论】:

    • 返回由制表符分隔的值可能会更好,现在您可能会面临数据中包含逗号会破坏事物的情况,但如果它永远不会那么它会起作用。
    • 感谢@SamiKuhmonen 的提示,test1 和 test2 只会是字母数字字符,所以这不是问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多