I have the following script to gather schema data and insert it into another database. I can run with no errors as a script defining variables.

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
        }

}
}

If I run it as function or parameterized it fails with an error:

Invoke-Sqlcmd : Cannot validate argument on parameter 'Database'. The argument is null or empty. Supply an argument tha t is not null or empty and then try the command again. At 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 and Invoke-Sqlcmd : Cannot validate argument on parameter 'Database'. The argument is null or empty. Supply an argument tha t is not null or empty and then try the command again. At line:47 char:104 + $InsertQueryOut = Invoke-Sqlcmd -Query $InsertQuery -ServerInstance $DDServerInstance -Database <<<< $DDData base + CategoryInfo : InvalidData: (:) [Invoke-Sqlcmd], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

share|improve this question
Where is $DDDatabase defined (it's not in the code you've given)? Are you passing it into the function as a parameter? Parameter validation & marking parameters as mandatory can help track these sorts of things down. – alroc Dec 18 '12 at 18:02
Yes. It's one of the defined parameters for the function and when I run it it's being passed. – Russ960 Dec 18 '12 at 19:26
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.