locked
How to control what happens when error is thrown. RRS feed

  • Question

  • User2116941655 posted

    Whats the code for error handling.

    Basicly i have a set of code that creates a table in sql databse.

    There cannot be duplicate tables and i want to tell it what to do when the duplicate table error is thrown (#  2714).

     

    is there somethng like

    If error 2714 then

        [ERROR CODE]

    end if.

     

    Thanks.

     

    Sunday, March 21, 2010 12:54 PM

Answers

  • User-525215917 posted

    If you are using SQL to create your table then you can check if table already exists. 

    IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='<table_name>')
    BEGIN
        <CREATE TABLE HERE>
    END



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 21, 2010 6:23 PM

All replies

  • User1011739529 posted

    Hi,

    You call easily create a stored procedure to check table existance before creating a new table, based on the result of it you may creating a new table or return error back to your user!

    follow the same issue:

    http://www.vbforums.com/showthread.php?t=532078

     

    wish it helps

     

    Sunday, March 21, 2010 3:08 PM
  • User2116941655 posted

    Yeah but it seems a bit too much.

    i was hoping to catch it with VisualBasic before it even posts.

     

    Something like:

     if exists(Sql table) then

         DO THIS

    else

     Dont DO THIS

    end if.

     

    Cant find the exact code though. im gonna keep looking and illl check back here soon. thanks for looking.

    Sunday, March 21, 2010 5:55 PM
  • User-525215917 posted

    If you are using SQL to create your table then you can check if table already exists. 

    IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='<table_name>')
    BEGIN
        <CREATE TABLE HERE>
    END



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 21, 2010 6:23 PM