0

i have a small powershell script that returns 3 values:

test.ps1

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

and i want to copy these 3 values into a postgresql table with the copy from program command:

copy mytable FROM PROGRAM 'powershell -executionpolicy remotesigned -File test.ps1'

the problem is that powershell seems to output the values in three lines:

2020-02-01

test1

test2

and the copy command is giving me the error:

"missing data for column col1" SQL-State: 22007

(sorry for not including the complete message but it isn't in english)

how do i solve this?

I tried this:

test.ps1

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

but this returns only one value and fails with the same message.

I guess i could write the output of the powershell script to a textfile and copy it from there but this doesn't seem to be a good solution because i still need to call the script from the sql script somehow.

Thank You

edit: mytable has 3 three columns: (date, col1, col2)

1 Answer 1

2

ok found the solution:

test.ps1

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

sql:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Probably better would be to return values separated by tab, now you risk a situation where having a comma in the data will break things, but if it never does then it will work.
thanks @SamiKuhmonen for the tip, test1 and test2 will only be alphanumeric characters so it isn't an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.