带参数的Powershell函数SQL存储过程

时间:2014-04-28 20:20:50

标签: sql sql-server powershell csv

收到的错误是“SqlParameterCollection只接受非空的SqlParameter类型对象,而不接受SqlCommand对象。” &安培; “过程或函数'usp__SingleUpdateServerBackupPath'需要参数'@decServerName',其中 没有提供。“ PowerShell代码:

Set-StrictMode -Version 1.0

function update-serverbackuppath {
param(
    [Parameter(Mandatory=$True,ValueFromPipeLine=$True)][object[]]$inputobject
)
BEGIN {
    $connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
    $connection.ConnectionString = "server=servername;database=database;trusted_connection=yes"
    $connection.Open()
}
PROCESS {

    $UpdateBackupPath = New-Object -TypeName System.Data.SqlClient.SqlCommand
    $UpdateBackupPath.Connection = $connection
    $UpdateBackupPath.CommandText = "usp__SingleUpdateServerBackupPath"
    $UpdateBackupPath.Commandtype = [System.Data.Commandtype]::StoredProcedure

    $ParamUpdateBackupPath = New-Object -TypeName System.Data.SqlClient.SqlParameter
    $ParamUpdateBackupPath.ParameterName = "@decBackupPath"
    $ParamUpdateBackupPath.SqlDbType = [System.Data.SqlDbType]::VarChar
    $ParamUpdateBackupPath.Direction = [System.Data.ParameterDirection]::Input
    $ParamUpdateBackupPath.Value = $inputobject.paths

    $ParamUpdateBackupPathServerName = New-Object -TypeName System.Data.SqlClient.SqlCommand
    $ParamUpdateBackupPathServerName.ParameterName = "@decServerName"
    $ParamUpdateBackupPathServerName.SqlDbType = [System.Data.SqlDbType]::VarChar
    $ParamUpdateBackupPathServerName.Direction = [System.Data.ParameterDirection]::Input
    $ParamUpdateBackupPathServerName.Value = $inputobject.names

    $UpdateBackupPath.Parameters.Add($ParamUpdateBackupPath)
    $UpdateBackupPath.Parameters.Add($ParamUpdateBackupPathServerName)

    $reader = New-Object -TypeName System.Data.SqlClient.SqlDataReader = $UpdateBackupPath.ExecuteReader()

}
END {
    $connection.Close()
}
}

SQL过程:

Create Procedure usp__SingleUpdateServerBackupPath
(
@decBackupPath AS varchar(50),
@decServerName AS varchar(50)
)
AS
UPDATE BCKP
SET PTH = @decBackupPath
FROM BCKP
INNER JOIN SRVR
    ON SRVR.ID = BCKP.FK_SRVR
WHERE SRVR.NM = @decServerName

CSV文件格式

Import-Csv -Path C:\Bin\Repos\Backup.csv | C:\Bin\Scripts\update-serverbackuppath.ps1
Names    Paths
Server1  \\fileshare\server_name

1 个答案:

答案 0 :(得分:0)

Powershell代码有几个语法错误,比如以错误的方式引用枚举:

# Incorrect form
[System.Data.Commandtype.StoredProcedure]
# Correct form for referring to enumeration
[System.Data.Commandtype]::StoredProcedure

稍后,有一个未声明的对象,其成员方法被调用:

# $command is not set, so ExecuteReader method is available
$reader = $command.ExecuteReader()

强烈建议在Powershell中use strict mode。它有助于通过阻止对未存在的属性访问未初始化的变量来捕获拼写错误。

修改

更新后的代码后,仍有两个错误:

# This doesn't make sense. The variable should be SqlParameter, not SqlCommand
$ParamUpdateBackupPathServerName = New-Object -TypeName System.Data.SqlClient.SqlCommand
# Like so:
$ParamUpdateBackupPathServerName = New-Object -TypeName System.Data.SqlClient.SqlParameter

# This is nonsense syntax
$reader = New-Object -TypeName System.Data.SqlClient.SqlDataReader = $UpdateBackupPath.ExecuteReader()
# Like so:
$reader = $UpdateBackupPath.ExecuteReader()