How do I call a SQL Server stored procedure from PowerShell?

前端 未结 6 1364
悲&欢浪女
悲&欢浪女 2020-12-07 17:04

I have a large CSV file and I want to execute a stored procedure for each line.

What is the best way to execute a stored procedure from PowerShell?

6条回答
  •  忘掉有多难
    2020-12-07 17:15

    I include invoke-sqlcmd2.ps1 and write-datatable.ps1 from http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/01/use-powershell-to-collect-server-data-and-write-to-sql.aspx. Calls to run SQL commands take the form:
    Invoke-sqlcmd2 -ServerInstance "" -Database -Query "truncate table

    "
    An example of writing the contents of DataTable variables to a SQL table looks like:
    $logs = (get-item SQLSERVER:\sql\).ReadErrorLog() Write-DataTable -ServerInstance "" -Database "" -TableName "
    " -Data $logs
    I find these useful when doing SQL Server database-related PowerShell scripts as the resulting scripts are clean and readable.

    提交回复
    热议问题