powershellazure-sql-databaseexport-to-csvbcp

How to run a stored procedure with parameters using bcp command against SQL Azure database


I have a stored procedure which needs 2 mandatory input parameters. I need to run that stored procedure using bcp command and export the results to a csv file.

Note: the stored procedure is hosted in a SQL Azure instance for which I only have Azure AAD Authentication to my account. UserName and Password Auth is not allowed.

This bcp command is running fine to export data from a table. but I'm unable to execute stored procedure with 2 input parameters.

bcp DatabaseName.domain.MyTableName 
    out V:\MyDocuments\SourceType.csv 
    -U viratkohli@conco.com -S Servername.database.windows.net -G -c -t","

What I am expecting to work for stored procedure:

bcp DatabaseName.dbo.MyStoreProcedure 'Parameter1','Parameter2' 
    out V:\MyDocuments\SourceType.csv 
    -U viratkohli@conco.com -S Servername.database.windows.net -G -c -t","

or:

$SqlQuery = "DatabaseName.dbo.MyStoreProcedure 'Parameter1','Parameter2'"
bcp $SqlQuery out V:\MyDocuments\SourceType.csv -U viratkohli@conco.com -S Servername.database.windows.net -G -c -t","

Solution

  • To run a stored procedure with parameters using bcp command against SQL Azure database follow below command:

    bcp "exec databas-ename.schema-name.SP-name 'Param1','Param2'" queryout "path of file" -S servername.database.windows.net -d dbname -U  username -G -c -t","
    

    Execution:

    enter image description here

    Output:

    enter image description here