Error handling using tsql

  • Hi All,

    Need some help in tsql error handling. Using SQL 2016 instance. I have 3 tables created. First off, I need to identify error records and redirect them custom error message and erroneous record id in the ErrorlogTable. I my case, error means ,column "deptno" is a primary key so it should'nt be NULL's , no duplicates and should be an integer value. the other 2 columns "Dname" & "location" shouldn't be a NULL. So, inside a stored procedure should be able to validate these and load the destination table. How to do that?

    Below are my table structures.

    CREATE TABLE [dbo].[DEPT_stg](
         [DEPTNO] [varchar](10),
         [DNAME] [varchar](30),
         [LOC] [varchar](10)
    )

    insert into DEPT_stg
    select NULL,'Dept1','Country1' --- //BAD RECORD: should be sent to error table
    union all
    select '10','SALES','DALLAS' --GOOD RECORD
    union all
    select '20','MARKETTING','US' --GOOD RECORD
    union all
    select '30','R&D','TEXAS' --GOOD RECORD
    union all
    select '40s','SALES','DALLAS' --- //BAD RECORD:should be sent to error table
    union all
    select '50',null,'xyz' --- //BAD RECORD: should be sent to error table
    union all
    select '60','ABC',NULL--- //BAD RECORD: should be sent to error table
    union all
    select '10','SALES','DALLAS' -- //BAD RECORD: duplicate record should be sent to error table

    select * from dept_stg

    CREATE TABLE [dbo].[DEPT_Errors](
         id int identity(1,1),
         [DEPTNO] [varchar](10),
         [DNAME] [varchar](30),
         [LOC] [varchar](10),
                 error_msg varchar(300),
                 loaddatettime datetime
    )

    CREATE TABLE [dbo].[DEPT_dest](
         [DEPTNO] [int] NOT NULL,
         [DNAME] [varchar](14) NULL,
         [LOC] [varchar](13) NULL
    )

    Thanks,
    Sam

  • You can't do row-level redirection in T-SQL. Inserts as an operation fail entirely.

    What you will need to do is clean the data before you insert it into the final tables, so from the staging, insert the rows that violate each error condition into the error tables, remove them from the staging table (or flag that they're error rows) and tehn, once you've run all the checks over the staging table, insert the valid rows into the destination.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 1st - add an identity column to your staging table.

    You need a way to distinguish between different duplicate rows.

    2nd, use that staging ID in Error table instead of its own identity column.

    3rd, create set of insert statements - 1per each type of errors, inserting rows from staging to error.

    4th, insert all the records from staging which don't have their ids recorder in error into destination table.

    5th, clean up staging. Use delete, not truncate, as it would reset identity, and you don't wanna do it.

    _____________
    Code for TallyGenerator

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

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