dimanche 26 juillet 2015

Trace exact column during error while debugging stored procedure

I work on support for a large and complex project based on SQL server 2005. It consists of more than 800 stored procedures and 1000 tables. Most SPs contain 2000+ lines and some have 10000+ lines of code. Each table contain more than 200 columns. Most of the SPs contain while loops, cursors, while loop in another cursor loop which is enormously complex. We use many temp tables, table variables with more than 200 columns I face difficulty when there is any error occurs. The most frequent errors are like below:

  1. String or binary data would be truncated.
  2. Arithmetic overflow error converting numeric to data type varchar .. and so on

It is extremely difficult to find out which column is occuring error. As the above error messages just show the error but not the actual column. Since there are more than 200 coloumns it has become a daunting task to find out the culprit. I have to comment/hardcode few columns and need to check if error is occuring or not.

Please find an example below for better understanding of my issue:

DECLARE @tablevar TABLE( Column1 int, column2 char(2),column char(6),.....
......,**Column137 char(7)**,..............column199 char(7), column(200) char(6))

......some code...

--while loop

while i < count  

Begin

--some code

Insert into @tablevar(Column1, column2,column3,.....
.....,**column137**...............column199, column200))

select t1.OthertableColumn1,t1.OthertableColumn2,t2.OthertableColumn3,t3.Othertablecolumn4,...
....,**t2.OthertableColumn137**,....

so on..........

t2.Column200)

from OtherTable t1 with (nolock)

LEFT JOIN OtherTable2 t2 with (nolock) on t1.col1 = t2.Column1

LEFT JOIN OtherTable3 t3 with (nolock) on t3.col1 = t2.Column1

LEFT JOIN OtherTable4 t4 with (nolock) on t4.col1 = t3.Column1

LEFT JOIN OtherTable5 t5 with (nolock) on t5.col1 = t1.Column1

LEFT JOIN OtherTable6 t6 with (nolock) on t6.col2 = t4.Column1

....
End

The actual error is occurring with the column Column137. The size of the column column137 of the table variable @tablevar is 7 characters but it is inserting more characters, for e.g. 'sample data'. So throwing the error - String or binary data would be truncated.

How do I identify this Column137 out of that large number of 200 columns? Is there any way of finding the specific column name where the error is occuring? Is there any shortcut/simple way to find out exact column? Your help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire