我创建了一个相当复杂的选择查询,它在我的几个 Dynamics GP 数据库中按预期工作,但我遇到了一个数据库,它抛出以下错误
“消息 8114,级别 16,状态 5,第 36 行 将数据类型 varchar 转换为数字时出错。”
我没有在第 36 行转换任何内容,(双击错误将我带到该行)它的字面意思是: “选择不同的 a.CUSTNMBR 作为‘客户编号’,”
该特定列主要是 varchar 值(字母和数字),但有一些值完全是数字。我认为它们会被视为 varchar 吗?我假设引用的行实际上不是触发错误的地方。
此查询适用于其他数据库(具有基本相同的模式)的事实确实令人困惑。我从连接到多个系统表的自定义视图(我没有创建)中提取,作为每个数据库中的基础数据源。我想知道这种观点是否是问题所在?在这个特定的数据库上,视图会有所不同吗?我不确定如何追踪它。生成的创建脚本在每个数据库上完全相同。
下面是实际的查询脚本
'Declare @MONTHEND as date
set @MONTHEND = '4/30/2023';
select distinct a.CUSTNMBR as 'Cust Num',
a.CUSTNAME as 'Cust Name',
a.DOCNUMBR as 'Document Num',
b.aaSubLedgerHdrID as 'Header ID',
a.posteddt as 'Posted Date',
case when a.RMDTYPAL = 1 then 'Invoice'
when a.rmdtypal = '9' then 'Payment'
when a.rmdtypal = '7' then 'Credit Memo'
when a.rmdtypal = '3' then 'Debit Memo'
else 'N/A'
end as 'Tran Type',
Case when a.rmdtypal in ('1','3') then a.ORTRXAMT*1
when a.rmdtypal in ('9','7') then a.ortrxamt*-1
else 'N/A'
end as 'Original Trx Amt',
Case when g.APFRDCTY = '7' then 0
when g.apfrdcty = '9' then sum(g.APFRMAPLYAMT)
else 0
end as 'Payments',
Case when g.apfrdcty = '7' then sum(g.APFRMAPLYAMT)
when g.apfrdcty = '9' then 0
else 0
end
as 'CR Memo',
Case when a.rmdtypal in ('1','3') then a.CURTRXAM*1
when a.rmdtypal in ('9','7') then a.CURTRXAM*-1
else 'N/A'
end as 'Curr Trx Amt',
c.aatrxdimcode as 'Project',
f.ACTNUMBR_2 as 'Natural',
sum(c.DR) as 'Debit',
sum(c.CR) as 'Credit'
from slbreceivablestrx a
left join AAG20000 b on a.DOCNUMBR = b.DOCNUMBR and a.RMDTYPAL=b.DOCTYPE
--left join AAG20001 c on b.aaSubLedgerHdrID = c.aaSubLedgerHdrID
--left join AAG20003 d on c.aaSubLedgerHdrID = d.aaSubLedgerHdrID and c.aaSubLedgerDistID = d.aaSubLedgerDistID and d.aaTrxDimID = (select aaTrxDimID from AAG00400 where aaTrxDim = 'PROJECT')
--left join AAG00401 e on d.aaTrxDimID = e.aaTrxDimID and d.aaTrxCodeID = e.aaTrxDimCodeID
left join (select a.aaSubLedgerHdrID,INTERID, ACTINDX, DISTTYPE, sum(debitamt) as "DR", sum(CRDTAMNT) as "CR", aaTrxDimCode from AAG20001 a
left join AAG20003 b on a.aasubledgerhdrid =b.aasubledgerhdrid and a.aaSubLedgerDistID=b.aaSubLedgerDistID and b.aaTrxDimID = (select aaTrxDimID from AAG00400 where aaTrxDim = 'PROJECT')
left join AAG00401 e on b.aaTrxDimID = e.aaTrxDimID and b.aaTrxCodeID = e.aaTrxDimCodeID
Group by a.aaSubLedgerHdrID, INTERID,ACTINDX,DISTTYPE,aaTrxDimCode) c on b.aaSubLedgerHdrID=c.aaSubLedgerHdrID
left join RM20201 g on a.DOCNUMBR = g.aptodcnm and a.CUSTNMBR=g.CUSTNMBR and g.ApplyFromGLPostDate <= @MONTHEND or a.DOCNUMBR=g.APFRDCNM and a.CUSTNMBR = g.CUSTNMBR and g.ApplyFromGLPostDate <= @MONTHEND
join GL00100 f on c.ACTINDX = f.ACTINDX
where /*a.CURTRXAM <> 0 and */ c.DISTTYPE = 3 and f.ACTNUMBR_2 like '%1295%' and a.GLPOSTDT <= @MONTHEND and a.VOIDSTTS <> 1
group by a.CUSTNMBR, a.CUSTNAME, a.DOCNUMBR, a.POSTEDDT, c.aaTrxDimCode, f.ACTNUMBR_2, a.RMDTYPAL, g.APFRDCTY, a.ORTRXAMT, a.CURTRXAM,g.apfrmaplyamt,b.aaSubLedgerHdrID`