Question

I'm confused:

The following code produces an error:

try {
    invoke-sqlcmd -ServerInstance 'localhost' -Database 'tempdb' -Query 'CREATE TABLE foo (bar TINYINT IDENTITY(1,1) DEFAULT 1);;' -Verbose -ErrorLevel 0 -AbortOnError -ErrorAction Stop -OutputSqlErrors $true -ErrorVariable $err -OutVariable $err -SeverityLevel 0;
    "OK";
}
catch {
    "ERROR"
    $_
}

Result:

ERROR Invoke-Sqlcmd : Defaults cannot be created on columns with an IDENTITY attribute. Table 'foo', column 'bar'.


This piece runs apparently smoothly:

try {
    invoke-sqlcmd -ServerInstance 'localhost' -Database 'tempdb' -Query 'SELECT CAST(111111111111 AS TinyINT) AS Error' -Verbose -ErrorLevel 0 -AbortOnError -ErrorAction Stop -OutputSqlErrors $true -ErrorVariable $err -OutVariable $err -SeverityLevel 0;
    "OK"
}
catch {
    "ERROR"
    $_
}

Result:

OK

In both cases the catch-block must be used!

Était-ce utile?

La solution

It's a known bug with invoke-sqlcmd. I logged it on Connect, the item is marked as fixed but "fixed" in connect terms doesn't necessarily mean the fix has been released or is planned for release in the current version. It just means that it has been fixed internally and they'll release it someday.

As of 4/19/2013, the fix has NOT been released.

Autres conseils

I suspect this has something to do with the configuration of the connection initiated by Invoke-SqlCmd; in particular, having ANSI_WARNINGS set to OFF in SQL Server allows your TSQL block to be evaluated as a NULL.

SET ANSI_WARNINGS OFF
SELECT CAST(111111111111 AS TinyINT) AS Error

Returns NULL.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top