I would like to find a quick method for debugging a insert-select statement.
Example:
Create table tbl_int (
tabid int identity,
col1 bigint)
Create table tbl_char(
tabid int identity,
col2 nvarchar(255))
insert into tbl_char(col2)
select '1' union
select '2' union
select 'a'
insert into tbl_int(col1)
select col2
from tbl_char
Of course, the insert select above fails to run and it is obvious that ‘a’ cannot be converted to bigint. But what happens when I have 1 milion records in tbl_char. Is there any way of finding the source value of the error:
“Error converting data type nvarchar to bigint.”
P.s. Using a convert or cast function and scanning the table with top until finding the right value is a little bit too expensive.
Why don’t you wrap the SQL that throw the exception into a Try/Catch block to have more info about it
the below are all the error information you can check
http://msdn.microsoft.com/en-us/library/ms179296.aspx
if this is not enough you can even write a query to select all the records where certain field is not convertible to a number(in your case there is a NVarChar which is not convertible)
The following example uses ISNUMERIC to return all the postal codes that are not numeric values.
http://msdn.microsoft.com/en-us/library/ms186272.aspx