我试图从一个服务器上的存储过程中获取结果,并通过另一个服务器上的存储过程将其插入作为数据表传递结果。当我这样做时,我收到此错误:
异常调用" ExecuteNonQuery"用" 0"参数:"无法将参数值从Object []转换为IEnumerable 1."
我认为尝试将数据表转换为可以传递给存储过程的东西会让人感到窒息,但我不确定这样做的正确方法是什么并且无法找到答案在线。
的PowerShell:
function Get-IndexBlitz
{
[CmdletBinding()]
param
(
[string] $server
)
Write-Host " Get Index Blitz"
$dt = GetBlitzResults $server
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Server=server_name; Database=Monitor_Reporting;Trusted_Connection=True;"
$sqlConnection.Open()
$sql = '[CentMang].[InsertIndexBlitzResults]'
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.Connection = $sqlConnection
$sqlCommand.CommandText = $sql
$sqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure
$sqlCommand.Parameters.Add("@server", [System.Data.SqlDbType]::VarChar).value = $server
$sqlCommand.Parameters.Add("@results", [System.Data.SqlDbType]::Structured).value = $dt
$sqlCommand.ExecuteNonQuery()
$sqlConnection.Close()
}
function GetBlitzResults
{
[CmdletBinding()]
param
(
[string] $server
)
Write-Host " Get Results"
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Server='$server'; Database=DBAdmin;Trusted_Connection=True;"
$sqlConnection.Open()
$sql = '[DBAdmin].[ozar].[sp_BlitzIndex]'
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.Connection = $sqlConnection
$sqlCommand.CommandText = $sql
$sqlCommand.CommandType = [System.Data.CommandType]::StoredProcedure
$sqlCommand.Parameters.Add("@Mode", [System.Data.SqlDbType]::TinyInt).value = 0
$sqlCommand.Parameters.Add("@GetAllDatabases", [System.Data.SqlDbType]::Bit).value = 1
$sqlCommand.Parameters.Add("@BringThePain", [System.Data.SqlDbType]::Bit).value = 1
$sqlCommand.Parameters.Add("@ThresholdMB", [System.Data.SqlDbType]::Int).value = 250
$Datatable = New-Object System.Data.DataTable
$DataReader = $sqlCommand.ExecuteReader()
$Datatable.Load($DataReader)
$sqlConnection.Close()
return $Datatable
}
存储过程:
CREATE PROCEDURE [CentMang].[InsertIndexBlitzResults]
@server varchar(512),
@results [CentMang].[BlitzIndexResults] readonly
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [Monitor_Reporting].[CentMang].[IndexBlitzResults]
(
[DateGathered]
,[ServerName]
,[Priority]
,[Finding]
,[DatabaseName]
,[Details]
,[Definition]
,[SecretColumns]
,[Usage]
,[Size]
,[MoreInfo]
,[URL]
)
select
getutcdate(),
@server as [server],
r.[priority],
r.finding,
r.databasename,
r.details,
r.[definition],
r.secretcolumns,
r.usage,
r.size,
r.moreinfo,
r.[url]
from @results r
END
GO
用户定义的表格类型:
CREATE TYPE [CentMang].[BlitzIndexResults] AS TABLE(
[priority] [int] NULL,
[finding] [nvarchar](max) NULL,
[databasename] [nvarchar](max) NULL,
[details] [nvarchar](max) NULL,
[definition] [nvarchar](max) NULL,
[secretcolumns] [nvarchar](max) NULL,
[usage] [nvarchar](max) NULL,
[size] [nvarchar](max) NULL,
[moreinfo] [nvarchar](max) NULL,
[url] [nvarchar](max) NULL
)