문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top