0

This is related to this question but slightly different, I have while loop that inserts records and I want it to continue even if some inserts fail. So, the insertrecords procedure inserts records, by doing a where on the temp table for top 50 rows at a time.

The problem is that it won't continue if any of the inserts inside the insertrecords fail? How can I modify the sql to continue with the next 50 rows, even if it fails for current 50 records. I guess is there something like try/catch exception handling in sybase?

 SELECT id INTO #temp FROM myTable 
    -- Loop through the rows of the temp table
    WHILE EXISTS(SELECT 1 FROM #temp)
    BEGIN
    BEGIN TRANSACTION
        exec insertrecords       
    IF @@error = 0
    begin
        print 'commited'
        commit
    end
    else
    begin
        print 'rolled back'
        rollback
    end
        DELETE TOP 50 FROM #temp order by id
    END
    -- Drop the temp table.
    DROP TABLE #temp
Community
  • 1
  • 1
Vishal
  • 12,133
  • 17
  • 82
  • 128

1 Answers1

-1

Try putting the content inside your while block inside try cactch.

NOTE: The below sample is in SQL, try similar code in sybase.

`WHILE(SOME CONDITION)

 BEGIN --start of while block

    BEGIN TRY-start of try block

      --your code here

    END TRY

     BEGIN CATCH

       PRINT ERR_MESSAGE();

       END CATCH

  END --end of while loop.
 `
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
  • 1
    I don't think `try` is a valid keyword in sybase. – Vishal Oct 15 '13 at 19:52
  • Your sample code look like SQL, and i have given a sql sample. Not sure it will work in sybase or not. – sudhansu63 Oct 16 '13 at 04:34
  • Yes, but I did mention sybase, and I know this will work in SQL Server. I was looking for something similar that works in Sybase. – Vishal Oct 16 '13 at 19:44
  • @sudhAnsu63: True. Sybase code looks a lot like SQL Server, since SQL Server is basically Sybase bought from Microsoft so that Microsoft could customize it afterwards. So, some basic stuff are the same in both Sybase and SQL Server. Besides, Sybase and SQL Server have their differences growing from versions to versions. Now, we may consider them as very different. Aside, some SQL standards are defined by SQL ANSI 89, 92, 2003, 2011 which somehow obliges every RDBMS to respect some standards, and they all have their personal flavour anyhow such as some system SPs, for instance. – Will Marcouiller Aug 01 '14 at 18:06