Results 1 to 6 of 6

Thread: Try Catch

  1. #1
    Join Date
    Apr 2006
    Posts
    178

    Try Catch

    Hi

    for MS SQL 2000
    how can I do ?:


    INSERT INTO [Users] (Name)
    SELECT Names FROM OtherUsers


    I am having an UNIQUE INDEX on [Users].Name

    how can I avoid an error ?
    if the [Users].Name allready exists I want to jump over the error and keep on inserting

    thank you for helping

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Try code like:

    INSERT INTO [Users] (Name)
    SELECT Names FROM OtherUsers
    where OtherUsers.Names not in (select Name from Users)

  3. #3
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    There is no TRY CATCH in SQL 2000, the sql batch terminates if it encounters unique key violation.

  4. #4
    Join Date
    Apr 2006
    Posts
    178
    is there any way to do the same thing than a try catch ?
    if error <> 0 then no matter !

    thak you rmiao it works but I am looking for a error handling to jump over errors

  5. #5
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    begin tran
    INSERT INTO OTHERUSERS SELECT 'b'

    IF @@ERROR=2601
    BEgin
    print 'rollback'
    ROLLBAck
    END
    else
    begin
    print 'commit'
    commit
    end

  6. #6
    Join Date
    Apr 2006
    Posts
    178
    thank you MAK

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •