Avoiding error

  • create table Empdata(empid int, Empname varchar(3), empsalary decimal(7,3))

    insert into Empdata values(1001,'LuckyAmerican',101)

    This will give an error as follows:

    Msg 2714, Level 16, State 6, Line 1

    There is already an object named 'Empdata' in the database.

    I don't want any error and the only 'Luc' to be inserted or whatever but no error should be thrown.

  • Hi,

    The error tells you that the table already in the database, you just drop the table and insert the record.

    And the table data type for the column Empname is only three characters; you may increase the data type length and insert the record.

  • You would test for the existence of the table before creating the table. Like this:

    IF OBJECT_ID('Empdata') IS NULL

    BEGIN

    create table Empdata

    (

    empid int,

    Empname varchar(3),

    empsalary decimal(7, 3)

    )

    END

    insert INTO Empdata

    values

    (

    1001,

    'LuckyAmerican',

    101)

Viewing 3 posts - 1 through 2 (of 2 total)

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