I am encountering data-conversion error on a table-insert and am baffled. I'm writing a script to do a quick-and-dirty reconciliation of freight costs from pre-consolidation and post-consolidation databases. I have two db tables created by importing CSV files. To keep things simple for the DBA, all the data from the CSV files will go into varchar columns in the tables - the recon scripts will handle conversion/casting as needed. The client is on SQL Server 2008 R2.
Here are abbreviated definitions for the tables in question:
CREATE TABLE [dbo].[NOCOMMIT_PROD](
[SORT_DELTA_TOTAL] [varchar](50) NULL,
[rec_type] [varchar](50) NULL,
[price_id] [varchar](50) NULL,
[quote_id] [varchar](50) NULL,
[shipto_org_id] [varchar](50) NULL,
...
[trace] [varchar](8000) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[NOCOMMIT_TEST](
[SORT_DELTA_TOTAL] [varchar](50) NULL,
[rec_type] [varchar](50) NULL,
[price_id] [varchar](50) NULL,
[quote_id] [varchar](50) NULL,
[shipto_org_id] [varchar](50) NULL,
...
[trace] [varchar](8000) NULL
) ON [PRIMARY]
Here is the temp-table definition and the Insert/Select statement that's erroring:
if OBJECT_ID('tempdb..#tmp1') is not null drop table #tmp1
create table #tmp1(
price_ID bigint
,quote_ID bigint
,shipTo_ID bigint
,product_ID bigint
,trmnl_ID bigint
,carrier_ID bigint
,test_freight_pricing_rt decimal(15,7)
,prod_freight_pricing_rt decimal(15,7)
,test_trace varchar(8000)
,prod_trace varchar(8000)
,test_carrierID_found bigint
,prod_carrierID_found bigint
,test_surcharge decimal(15,7)
,prod_surcharge decimal(15,7)
,test_xtra_surcharge decimal(15,7)
,prod_xtra_surcharge decimal(15,7)
,test_miles int
,prod_miles int
,test_rate decimal(15,7)
,prod_rate decimal(15,7)
,explanation varchar(400)
)
insert into #tmp1(
price_ID
,quote_ID
,shipTo_ID
,product_ID
,trmnl_ID
,carrier_ID
,test_freight_pricing_rt
,prod_freight_pricing_rt
,test_trace
,prod_trace
)
select
cast(p.price_ID as bigint)
,cast(p.quote_ID as bigint)
,cast(p.shipto_org_id as bigint)
,cast(q.product_id as bigint)
,cast(p.cust_term_relation_id as bigint)
,cast(p.carrier_org_ID as bigint)
,cast(t.freight as decimal(15,7))
,cast(p.freight as decimal(15,7))
,t.trace
,p.trace
from NOCOMMIT_PROD p
join NOCOMMIT_TEST t on p.price_id = t.price_id
join tbl_fuel_cost_quote q on q.quote_id = p.quote_id
join tbl_fuel_cust_price cp on cp.price_id = p.price_id
where p.price_id != 'NULL' and p.price_id <> -1
When I execute the above, it returns "Msg 8114, Level 16, State 5, Line 54 Error converting data type varchar to numeric.". If I comment out the insert of the prod_trace column, it executes just fine. I'm baffled by this, since the source for prod_trace is the NOCOMMIT_PROD.trace columns (same size and type). I cannot find anything wrong with the source data. Can someone please provide insight on why I'm getting this error when there is no numeric involved?
Thanks for any help and insight you can furnish!!