views:

258

answers:

3

Hey.. if i try to run following code in sql server 2005 i get error

BEGIN TRY

        SELECT 1/0;
    END TRY
    BEGIN CATCH
        SELECT
            ERROR_NUMBER() AS ErrorNumber
            ,ERROR_SEVERITY() AS ErrorSeverity
            ,ERROR_STATE() AS ErrorState
            ,ERROR_PROCEDURE() AS ErrorProcedure
            ,ERROR_LINE() AS ErrorLine
            ,ERROR_MESSAGE() AS ErrorMessage;
    END CATCH;
    GO

Error:

Line 1: Incorrect syntax near 'TRY'.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'END'.
Msg 195, Level 15, State 10, Line 7
'ERROR_NUMBER' is not a recognized function name.

i was using sql server 2000 service manager but now i have installed sql server 2005 service manager but still get the error..one article related to it says

the reason for a such error is seems to be the target database is running on a MS SQL Server 2000 server. So be sure that your database server is SQL2005

what does it mean..?what changes i have to do???

+6  A: 

You need to be using SQL Server 2005 or above to be able to use TRY...CATCH, installing SQL Server Management Studio will not help if you are still connecting to a SQL 2000 server.

Rory
hey got it..thanx a ton..!!!
anay
A: 
BEGIN TRY
     { sql_statement | statement_block }
END TRY
BEGIN CATCH
          [ { sql_statement | statement_block } ]
END CATCH

This is proper structrure of try and catch in sql 2005..

so... edit your statement into this..

end check your answer..

thanks..

Sikender
yes..that the same format i hv used..isnt it??
anay
it is rignt one.. but you make a semicolon after last catch after end.check it out
Sikender
i tried it but still getting same error...i also hv sql server 2000 installed on my machine...so is problem lies there??
anay
The semicolon isn't the problem, his syntax is correct. In fact, he copied that example straight out of SQL Books Online (I read it earlier today).Anay, take a look at the answer I posted earlier, even though you have SSMS 2005 installed, you are still connecting to you 2000 Server. The TRY...CATCH syntax is a SERVER feature, you need to connect to a 2005 or 2008 server to be able to use it.
Rory
@Rory...yes i figured that out...but how to do it when i open sql server 2005 i get connected automatically..i dont know which server it uses..my question is how do i change this..?? thanx
anay
uninstall sql server 2000 dude.. and 2005 also. again.. install sql server 2005 and then check it out..ok..
Sikender
@anay: You seem to not be listening to the answers. You cannot use try..catch in SQL sent to a SQL Server 2000 database, since that database doesn't support it. It doesn't matter how you connect to that database; it's still a SQL Server 2000 database. In order to get support for try..catch, you have to +change the database+. There's no other way.
Ken White
P.S. If you are running two instances of SQL Server on the same machine, you need to specify which instance to connect to at the login screen (or in your connection string). Instead of just <server name>, you should specify <server name>\<instance name> (by default this will be "localhost\sql2005" for a SQL 2005 server running on your local machine). If all else fails, stop the SQL 2000 service before trying to connect, if your connection fails, you're connecting to the wrong server.
Rory
A: 

the reason for a such error is seems to be the target database is running on a MS SQL Server 2000 server. So be sure that your database server is SQL2005

Mike C.
@Mike C.: You didn't notice the other people who had already answered with the same info?
Ken White
I was pointing out that he answered his own question in the original post.
Mike C.
@Mike..yes i know the reason..but i dont know how do i solve the problem...
anay
@Mike: So did the rest of us. @anay seems determined to not listen to the answers, though.
Ken White