How to get detailed error messages. Include table & column names & records. Conversion failed when converting the varchar value 'A' to data type int
0
votes
1
answer
107
views
I have an old, large (more than 140 lines), and complicated SQL query. It is joining many tables and has detailed WHERE conditions with values and a detailed GROUP BY part.
**Query Structure**
SELECT
H.C1
H.C2
H.C3
D.C1
D.C2
D.C3
C.C1
C.C2
F.C1
F.C2
F.C3
FROM TBL1 H
LEFT JOIN TBL2 D ON D.WH_ID = H.WH_ID
LEFT JOIN TBL3 C ON H.CUST_ID = C.ID
LEFT JOIN TBL4 F ON H.RECEIPT_ID = F.ID
AND H.CUST_ID = F.CUST_ID
WHERE
DETAILED WHERE CONDITIONS
GROUP BY
GROUP COLUMNS
ORDER BY
ORDER BY COLUMNS
**Error message**
It gives
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value 'A' to data type int.
Completion time: 2024-09-03T09:41:07.2716887+03:00
It is a superficial error messages, for which the specific table and column or the specific where condition the error is related to is an unknown.
Moving step by step, I solved the problem, but the SQL Server Engine knows the table and column name that are related to the problem. Why doesn't it give this info in the error message?
**Using Catch Block**
BEGIN TRY
SELECT
H.C1
H.C2
H.C3
D.C1
D.C2
D.C3
C.C1
C.C2
F.C1
F.C2
F.C3
FROM TBL1 H
LEFT JOIN TBL2 D ON D.WH_ID = H.WH_ID
LEFT JOIN TBL3 C ON H.CUST_ID = C.ID
LEFT JOIN TBL4 F ON H.RECEIPT_ID = F.ID
AND H.CUST_ID = F.CUST_ID
WHERE
DETAILED WHERE CONDITIONS
GROUP BY
GROUP COLUMNS
ORDER BY
ORDER BY COLUMNS
END TRY
BEGIN CATCH
SELECT
ERROR_MESSAGE() AS ErrorMessage
,ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
END CATCH;
GO
I tried to get detailed error messages with a Catch block but the result was the same.
**Is it possible to configure SQL Server (may be with trace flags) or SSMS to give a detailed error messages, including related tables and columns?**


Asked by Fevzi Kartal
(338 rep)
Sep 3, 2024, 07:43 AM
Last activity: Sep 4, 2024, 05:05 AM
Last activity: Sep 4, 2024, 05:05 AM