insert records into table which record insert wrong that record only rollback remains insert

  • hi friend i have small doubt in sql server plz tell me how to solve issue

    i want insert records into emp table from 1 t0 3 records. emp table contains only id column and datatype is int

    whenever i insert records that time any errore occured that record will be skiped and remaing records loaded into table

    i mean supose 1st and 3rd records insert correctly and 2nd record insert wrong way either select table wrong or insert record value wrong

    i try query like bellow

    begin tran

    insert into emp values(1)

    if(@@errore<>0)

    rollback tran

    save tran s1

    insert into emp12 values(2)

    if(@@errore<>0)

    rollback tran s1

    save tran s2

    insert into emp values(3)

    commit tran

    here i wrongly insert emp12 table record.

    where ever i insert inncorrect format that records only rollback and remaing records must be loaded into emp table

    once i execute above query only 1st recor inserted and 3rd record not inserted into emp table

    but i want insert 3rd record also. i want out put like id

    1

    3

    plz tell me how to solve this issuse in sql server.plz tell me that query in sql server.

  • Try looking at this problem differently:

    You want to insert some rows into a table [emp]. Any rows which will fail to insert into [emp] (e.g. duplicate key), should be inserted into a different table.

    This description is very different to yours, but the end state is the same. It's also much easier to understand and code up.

    Can you confirm that this is what you are trying to do?

    What errors might you expect, inserting data into [emp]?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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