TRY…CATCH Construct

  • hello,

    i recently discovered that sql server supports this  costruct,

    http://msdn2.microsoft.com/en-us/library/ms175976.aspx.

    My problem is: i can't use it.

    if i try  a simple try...catch :

    BEGIN

    TRY

    SELECT 1/0

    END

    TRY

    BEGIN

    CATCH

    SELECT 1

    END

    CATCH;

    i get this error:

    Msg 170, Level 15, State 1, Line 1

    Line 1: Incorrect syntax near 'TRY'.

    Msg 156, Level 15, State 1, Line 5

    Incorrect syntax near the keyword 'END'.

     

  • hi

    i copied the statements as it is...it seems to be working.

    "Keep Trying"

  • probably i undertud the problem: in a notebook with sql standard ed. it works because db version is 9.0....

    in the notebook with sql enterprise ed it doesn't work because db version is 8...

     

    how can i update from 9 to 8 the ent.se  edition?

  • v8 is SQL Server 2000.  If you're running SQL Server 2000 then try/catch won't work and you have to look at the @@ERROR global variable/function.

    However, if you are running SQL 2005 then you might be running your database in SQL 2000 (ie, v8) compatibility mode.  This means that your databse will run in SQL 2005 but SQL 2005 will implement some functions differently, and disallow others, to make the SQL syntax and behaviour similar to that experienced under SQL 2000.

    To change this, open up 2005 management studio.  Right click the database and bring up its properties. In this window there are several sections - one of these lets you specify the database compatibility level - change this and see if your code starts working again.  Note that if you want your code to work in both 2000 and 2005 environments then you'll want to avoid try/catch (despite how nice it is!)

    Cheers.

  • Use sp_dbcmptlevel to check and/or change the compatibility level of databases.


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply