TRY CATCH IN SQL SERVER 05

  • I am using following code to implement try n catch in sql server 2005. But its giving me error of "Invalid syntax near TRY".....is there some thing i m doin wrong?

    BEGIN TRY

    BEGIN TRANSACTION

    INSERT Funds VALUES (10)

    INSERT Funds VALUES (-1)

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    ROLLBACK

    END CATCH

  • it parses fine for me,

    is this the whole query?

  • yes this is the whole query.

    Do i have to enable exception handling?

    coz in my Query window.......TRY & CATCH are not being shown as Reserved words.

  • you dont have to enable it, TRY should turn blue once you have typed it in SMSS, unless you are using 2k then it won't work at all

  • I am using sql server 2005 Developer Edition. TRY & Catch are not truning into BLUE (Reserved Words).

    By the way i am running it as an anynomas blocks...could dat be a problem?

    Should i use it in a procedure?

  • By the way i am running it as an anynomas blocks...could dat be a problem?

    Should i use it in a procedure?

    I think anyomous blocks are an ORACLE term, i have never heard of it in SQL. I may be wrong though.

    This should be run as a procedure, also the query does not look like it would run anyway.

    is Funds a column in a table?

    if so you need to tell the query what table to insert the data into.

  • Anynomas block means running the query in SSMS...not within a procedure or function.

    i have tried it as follows now....but its still now working......p.s Funds is the name of the table

    create proc abc

    as

    begin

    BEGIN TRY

    BEGIN TRANSACTION

    INSERT INTO Funds VALUES (10)

    INSERT INTO Funds VALUES (-1)

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    ROLLBACK

    END CATCH

    end

  • Thanks, i havent heard that term before...

    The query works fine for me and inserts two rows into the table.

  • HI There,

    What compatability mode are you in 80 or 90?

    Thanks

    Chris

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • sorry one more question might be a silly one.

    What console are you using?

    e.g. SSMS or Query Analyser etc?

    Thanks

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • One last thing this can be run as a block and will work , it should not have to be in a Procedure...

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Sounds like a client side issue. If you are using SMSS, do you have Tools > Options > Scripting > "Script for server Version" ... make sure its set to SQL Server 2005. I'm not sure waht else the setting would be good for.

  • I am using SQL Server 2005 Management Studio.

    I think there will be a setting to enable the TRY n CATCH stuff.

    @steveB....which version of SQL Server r u using?enterprise?development?wat service pack?

  • @Noffer........i cant find "scripting" option in Tools-->Options

    There is no such option available

  • There is no option to activate/deactivate Try/Catch in SQL 2005.

    Right-click on the database in Management Studio, select Properties, go to the Options tab, there will be a line on there that says what compatibility level you are running in. If it says "80" instead of "90", you will not be able to use any of the SQL 2005 features, including Try/Catch, in that database.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 15 posts - 1 through 15 (of 18 total)

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