sql失败转换为int

时间:2012-08-23 07:54:43

标签: sql sql-server-2008

SQL server Manager 2008。

我最近更改了一个存储过程来查看另一个表。

从那时起,我一直收到同样的错误:

“Msg 245,Level 16,State 1,Line 1 将varchar值'BC8018'转换为数据类型int时,转换失败。“

现在我将查询分解到根目录以查找此问题发生的位置,并确定它在JOIN上发生到我的新表。

现在我比较了表中使用的唯一字段,Pareto和NewPareto都是整数。

下面是代码:

SELECT i.Acct,
   i.Name, 
   i.Document, 
   i.Part, 
   i.Qty, 
   i.Unit, 
   dbo.PeriodInsert.NewPareto,
   i.pg,
   i.[DateTime],
   i.BinSeqNo

FROM   
OPENQUERY(SACBAUTO, 'SELECT dbo.iHeads.acct,
                            dbo.iHeads.name,
                            dbo.iLines.Document,
                            dbo.iLines.Part,
                            dbo.iLines.Pg,
                            dbo.iLines.Qty,
                            dbo.iLines.unit,
                            dbo.iHeads.[DateTime], 
                            dbo.iLines.BinSeqNo 
                     FROM Autopart.dbo.iheads INNER JOIN Autopart.dbo.iLines ON 
                     Autopart.dbo.Iheads.document = autopart.dbo.iLines.document
                     WHERE dbo.iLines.Pg not in (''60'',''61'',''62'')

                      ') i
left JOIN
dbo.PeriodInsert
ON 
i.Part collate SQL_Latin1_General_CP1_CI_AS = dbo.PeriodInsert.NewPareto 
WHERE
 (i.[DateTime] BETWEEN '2012-03-01' AND '2012-08-23') AND
 i.Acct = '1557' 

请记住这是我对原始存储过程的分解,但这是它的根源发生的地方。我觉得很奇怪,使用字段pareto的原始表名为NEWpareto也是一个int。 有没有想过为什么这件事因桌子而突然停止工作?

如果您需要更多信息,请告诉我。

非常感谢

1 个答案:

答案 0 :(得分:1)

dbo.iLines.Part不是int - 它有一个排序规则 - 所以如果NewPareto是,那么你试图将它转换为int来进行连接。在该字段中搜索“BC8018”并检查其是否有效。如果是,那么您必须加入i.Part加入convert(nvarchar(...), dbo.PeriodInsert.NewPareto)

相关问题