Powershell函数中的“Argument null或empty”错误使用Invoke-SQLCmd

时间:2012-12-18 17:58:19

标签: sql-server powershell powershell-v2.0

我有以下脚本来收集架构数据并将其插入另一个数据库。作为定义变量的脚本,我可以毫无错误地运行。

Function LoadDataDictionary ($SourceServerInstance, $DDServerInstance, $SourceDatabase, $DDDatabase, $DDTableName)
{

$tablelist = Invoke-Sqlcmd -Query "SELECT DISTINCT table_name FROM INFORMATION_SCHEMA.COLUMNS ORDER BY Table_name" -ServerInstance $SourceServerInstance -Database $SourceDatabase

foreach($tables in $tablelist)
{
$Query = "SELECT tbl.name AS `"Table_Name`",
clmns.name AS `"Column_Name`", 
substring(ISNULL(CAST(exprop.value AS VARCHAR(255)),`'`'),1,250) AS `"Ext_Prop`",
CAST(ISNULL(idxcol.index_column_id, 0)AS VARCHAR(20)) AS `"Is_Primary_Key`",
CAST(ISNULL((SELECT TOP 1 1 FROM sys.foreign_key_columns AS fkclmn WHERE fkclmn.parent_column_id = clmns.column_id AND fkclmn.parent_object_id = clmns.object_id), 0) AS VARCHAR(20)) AS `"Is_Forign_Key`",
CAST(udt.name AS CHAR(15)) AS `"DataType`" ,
CAST(CAST(CASE WHEN typ.name IN (N`'nchar`', N`'nvarchar`') AND clmns.max_length <> -1 THEN clmns.max_length/2 ELSE clmns.max_length END AS INT) AS VARCHAR(20)) AS `"Length`",
CAST(CAST(clmns.precision AS INT) AS VARCHAR(20)) AS `"Numeric_Precision`",
CAST(CAST(clmns.scale AS INT) AS VARCHAR(20)) AS `"Numeric_Scale`",
CAST(clmns.is_nullable AS VARCHAR(20)) AS `"Nullable`" ,
CAST(clmns.is_computed AS VARCHAR(20)) AS `"Computed`" ,
CAST(clmns.is_identity AS VARCHAR(20)) AS `"Is_Identity`" ,
isnull(REPLACE(CAST(cnstr.definition AS VARCHAR(20)), `'`'`'`',`'`'),`'`') AS `"Default_Value`"
FROM sys.tables AS tbl
INNER JOIN sys.all_columns AS clmns
ON clmns.object_id=tbl.object_id
LEFT OUTER JOIN sys.indexes AS idx
ON idx.object_id = clmns.object_id
AND 1 =idx.is_primary_key
LEFT OUTER JOIN sys.index_columns AS idxcol
ON idxcol.index_id = idx.index_id
AND idxcol.column_id = clmns.column_id
AND idxcol.object_id = clmns.object_id
AND 0 = idxcol.is_included_column
LEFT OUTER JOIN sys.types AS udt
ON udt.user_type_id = clmns.user_type_id
LEFT OUTER JOIN sys.types AS typ
ON typ.user_type_id = clmns.system_type_id
AND typ.user_type_id = typ.system_type_id
LEFT JOIN sys.default_constraints AS cnstr
ON cnstr.object_id=clmns.default_object_id
LEFT OUTER JOIN sys.extended_properties exprop
ON exprop.major_id = clmns.object_id
AND exprop.minor_id = clmns.column_id
AND exprop.name = `'MS_Description`'
WHERE tbl.name = `'" + $tables.table_name + "`'
ORDER BY tbl.name, clmns.column_id ASC"

$QueryOut = Invoke-Sqlcmd -Query $Query -ServerInstance $SourceServerInstance -Database $SourceDatabase

    foreach($QueryOutData in $QueryOut)
        {
        $InsertQuery = "INSERT INTO $DDTableName ([DatabaseName], [Table Name], [Column Name], [Ext Prop], [Is Primary Key], [Is Forign Key], [DataType], [Length], [Numeric Precision], [Numeric Scale], [Nullable], [Computed], [Is Identity], [Default Value]) VALUES (`'$SourceDatabase`',`'" + $QueryOutData.Table_Name + "`', `'" + $QueryOutData.Column_Name + "`', `'" + $QueryOutData.Ext_Prop + "`', `'" + $QueryOutData.Is_Primary_Key + "`', `'" + $QueryOutData.Is_Forign_Key + "`', `'" + $QueryOutData.DataType + "`', `'" + $QueryOutData.Length + "`', `'" + $QueryOutData.Numeric_Precision + "`', `'" + $QueryOutData.Numeric_Scale + "`', `'" + $QueryOutData.Nullable + "`', `'" + $QueryOutData.Computed + "`', `'" + $QueryOutData.Is_Identity + "`', `'" + $QueryOutData.Default_Value + "`')"
        $InsertQueryOut = Invoke-Sqlcmd -Query $InsertQuery -ServerInstance $DDServerInstance -Database $DDDatabase
        }

}
}

如果我将其作为函数或参数化运行,则会失败并显示错误:

  

Invoke-Sqlcmd:无法验证参数'Database'的参数。该   参数为null或空。提供一个参数,它不是null或   为空,然后再次尝试该命令。在行:43 char:88       + $ QueryOut = Invoke-Sqlcmd -Query $ Query -ServerInstance $ SourceServerInstance -Database&lt;&lt;&lt;&lt; $ SourceDatabase           + CategoryInfo:InvalidData:(:) [Invoke-Sqlcmd],ParameterBindingValidationException           + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.SqlServer.Management.PowerShell.GetScriptComm   和Invoke-Sqlcmd:无法验证参数'Database'的参数。   参数为null或空。提供一个参数,它不是null或   为空,然后再次尝试该命令。在行:47 char:104       + $ InsertQueryOut = Invoke-Sqlcmd -Query $ InsertQuery -ServerInstance $ DDServerInstance -Database&lt;&lt;&lt;&lt; $ DDData基础           + CategoryInfo:InvalidData:(:) [Invoke-Sqlcmd],ParameterBindingValidationException           + FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

0 个答案:

没有答案