SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error handling using tsql


Error handling using tsql

Author
Message
vsamantha35
vsamantha35
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6324 Visits: 1096

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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (734K reputation)SSC Guru (734K reputation)SSC Guru (734K reputation)SSC Guru (734K reputation)SSC Guru (734K reputation)SSC Guru (734K reputation)SSC Guru (734K reputation)SSC Guru (734K reputation)

Group: General Forum Members
Points: 734421 Visits: 48476
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


Sergiy
Sergiy
SSC Guru
SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)SSC Guru (78K reputation)

Group: General Forum Members
Points: 78769 Visits: 13538
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search