0

I am trying to run a BCP command in windows machine powershell and unable to give input from a sql file.

Below command works fine in my MAC and I am looking for the same for Windows machine.

bcp "$(< /Users/mahendra.hegde/Desktop/input.sql)" queryout /Users/mahendra.hegde/dataout.json -c -T -S sample.env.compxyz.com

input.sql file scripts should be passed as input to bcp command when it runs.

I tried many ways in Windows but did not work, any help would be appreciated

Thanks

MHegde
  • 329
  • 3
  • 14
  • This is in bash on the Mac? If so, please provide a breakdown and explanation of what it's doing. – Jeff Zeitlin Mar 08 '21 at 18:16
  • Are you just reading the content of a file? please be clear on what you want to achieve – Isaac Mar 08 '21 at 18:20
  • What did you search for? ['bcp powershell'](https://duckduckgo.com/?q=%27bcp+powershell%27&t=h_&ia=web) --- What have you tried? What happened? [• PowerShell: Running Executables](https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx). There are modules to work wiht SQL via teh MS powershellgallery.com. `Find-Module -Name '*sql*' | Format-Table -AutoSize` – postanote Mar 08 '21 at 18:30
  • SO does have rules we are told to follow: [Provide MRE](https://stackoverflow.com/help/minimal-reproducible-example) --- [How to ask](https://stackoverflow.com/help/how-to-ask) --- [Don't ask](https://stackoverflow.com/help/dont-ask) --- [Proper Topic](https://stackoverflow.com/help/on-topic) --- [Why not upload images of code/errors?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) --- [format your post properly](https://stackoverflow.com/help/formatting): – postanote Mar 08 '21 at 18:31

1 Answers1

2

The equivalent of this bash command:

bcp "$(< /Users/mahendra.hegde/Desktop/input.sql)" queryout

is this PowerShell command:

bcp (Get-Content -Raw C:/Users/mahendra.hegde/Desktop/input.sql) queryout

Both commands pass the content of the specified file as a single, multiline string to the bcp utility; see the Get-Content cmdlet's documentation.

However, as of PowerShell 7.1, due to broken passing of arguments with embedded " characters - see this answer - more work is needed if the .sql file contains " characters; in the simplest case:

bcp ((Get-Content -Raw C:/Users/mahendra.hegde/Desktop/input.sql) -replace '"', '\"') queryout
mklement0
  • 382,024
  • 64
  • 607
  • 775