在SQL Server中尝试捕获

ANP*_*ANP 6 sql-server try-catch sql-server-2008

我正在使用SQL Server 2008.我试图执行以下操作:

BEGIN TRY
    SELECT 1/0;
END TRY
BEGIN CATCH
    PRINT 'ERROR'
END CATCH;
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

>Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'TRY'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'END'.
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何在SQL Server中执行try catch?

KM.*_*KM. 9

对于SQL Server 2005及更高版本,这是一个完全有效的语句,因此我将使用sp_dbcmptlevel(Transact-SQL)检查您的兼容级别:

exec sp_dbcmptlevel 'YourDatabaseName'
Run Code Online (Sandbox Code Playgroud)

80 = SQL Server 2000
90 = SQL Server 2005
100 = SQL Server 2008

我认为它会返回80或更低,似乎它不知道BEGIN TRY,只有BEGIN.这BEGIN TRY是在SQL Server 2005中添加的.