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 : パラメーター 'データベース' の引数を検証できません。引数が null または空です。null または空でない引数を指定して、コマンドを再試行してください。行:43 文字:88 + $QueryOut = Invoke-Sqlcmd -Query $Query -ServerInstance $SourceServerInstance -Database <<<< $SourceDatabase + CategoryInfo : InvalidData: (:) [Invoke-Sqlcmd], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError, Microsoft.SqlServer.Management.PowerShell.GetScriptComm および Invoke-Sqlcmd : パラメーター 'Database' の引数を検証できません。引数が null または空です。null または空でない引数を指定して、コマンドを再試行してください。行:47 文字:

4

0 に答える 0