我有以下脚本来收集模式数据并将其插入另一个数据库。作为定义变量的脚本,我可以毫无错误地运行。
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:无法验证参数“数据库”上的参数。参数为 null 或空。提供一个不为空或不为空的参数,然后再次尝试该命令。在 line:43 char:88 + $QueryOut = Invoke-Sqlcmd -Query $Query -ServerInstance $SourceServerInstance -Database <<<< $SourceDatabase + CategoryInfo : InvalidData: (:) [Invoke-Sqlcmd], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError, Microsoft.SqlServer.Management.PowerShell.GetScriptComm 和 Invoke-Sqlcmd:无法验证参数“数据库”上的参数。参数为 null 或空。提供一个不为空或不为空的参数,然后再次尝试该命令。在行:47 字符: