简体   繁体   English

为什么 SQL 服务器不能告诉我是哪个列导致了错误

[英]Why can't SQL Server tell me which column is causing the error

I'm running a pretty standard我正在运行一个非常标准的

INSERT INTO [table] (col1, col2, ...coln)
select * from #temp

and I'm getting the following error:我收到以下错误:

Msg 245, Level 16, State 1, Line 1消息 245,16 级,State 1,第 1 行
Conversion failed when converting the varchar value 'NULL' to data type int.将 varchar 值“NULL”转换为数据类型 int 时转换失败。

I understand the error, but want to know why the column that is causing issue isn't identified by the error message.我了解该错误,但想知道导致问题的列为何未由错误消息标识。 Is there an easy way to find which column contains the naughty null or is this just a ploy to make me look like I'm being productive at work while I really just spent 30 minutes looking at a huge result set without getting anywhere?有没有一种简单的方法可以找到哪一列包含顽皮的 null,或者这只是一种让我看起来工作效率很高的策略,而实际上我只是花了 30 分钟查看一个巨大的结果集而一无所获?

Edit: Thanks for the help guys, but no one really answered the question.编辑:感谢您的帮助,但没有人真正回答这个问题。 Do all RDBMS's spew out similar error messages are or some more helpful?所有 RDBMS 发出的类似错误消息是否更有用? Its 2012...trial and error over possibly thousands of columns should be dead!它的 2012 年......对可能数以千计的专栏的反复试验应该已经死了!

I would look at how you populate the temp table. 我会看看你如何填充临时表。 You appear to be getting a value of 'null' not NULL. 您似乎获得的值为“null”而不是NULL。 If this data is coming from a Excel file, this is a common problem. 如果此数据来自Excel文件,则这是一个常见问题。 I usually clease the data first by updating this way: 我通常通过这种方式更新数据:

Update #temp
set field1 = NULL
where field1 = 'NULL'

If you want to do all in the same update command, then 如果要在同一个更新命令中执行所有操作,那么

Update #temp
set field1 = NULLIF(field1, 'NULL')
, field2 = NULLIF(field2, 'NULL')
, field3 = NULLIF(field3, 'NULL')

It shouldn't take you 30 minutes to figure out where the null is. 它不应该花30分钟来确定null的位置。 You only have so many columns. 你只有这么多列。 Just start selecting from #temp WHERE col1 IS NULL, then WHERE col2 is. 刚开始从#temp中选择WHERE col1 IS NULL,然后是WHERE col2。

If #temp has a VARCHAR column you're trying to put into in INT column then cast it. 如果#temp有一个VARCHAR列,你试图将其放入INT列然后进行转换。 If there are NULLs you might want to handle them with an CAST(ISNULL(VarCharColumn, '0') AS INT) or something. 如果有NULL,您可能希望使用CAST(ISNULL(VarCharColumn,'0')AS INT)处理它们。 If an INT column allows NULLS, then just the cast to INT should be enough (as long as all the values are NULL or a valid int). 如果INT列允许NULLS,那么只需要转换为INT就足够了(只要所有值都是NULL或有效的int)。

If you write your INSERT with a little bit more care then you should be able to get the results you want. 如果你更谨慎地编写INSERT,那么你应该能够得到你想要的结果。

If you run these two statements before your query you will see null values on those columns in the results set:如果您在查询之前运行这两个语句,您将在结果集中的这些列上看到 null 个值:

SET ARITHABORT OFF 
SET ANSI_WARNINGS OFF

You're going to need some trial and error as @Jeremy pointed out. @Jeremy指出,你需要一些试验和错误。 But you can winnow down the choices. 但你可以放下选择。

The error message says that the problem is a NULL in a varchar column. 错误消息表明varchar列中的问题是NULL。 You can restrict your searching to just the varchar columns in #temp: select * from #temp where col1 is null or col3 is null 您可以将搜索限制为#temp中的varchar列: select * from #temp where col1 is null or col3 is null

Second, the problem is also happening when the database engine tries to convert a null varchar value to an integer not null . 其次,当数据库引擎尝试将null varchar值转换integer not nullinteger not null时,也会出现问题。 Compare the definitions of both tables to see where a varchar in #temp matches up with an integer not null in the other table. 比较两个表的定义,以查看#temp中的varchar与另一个表中的非integer not null匹配的位置。

This, however, is suspicious. 然而,这是可疑的。 Why are you trying to convert text to numbers? 为什么要尝试将文本转换为数字? If that's what you really want to do, you will probably need an explicit cast from textual to numeric. 如果这是你真正想做的事情,你可能需要从文本到数字的显式转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 postgres 告诉哪个列导致错误 - How to make postgres tell which column is causing an error 告诉我不需要SQL Server索引的情况 - Tell me a situation in which SQL Server indexing is not required 有人可以告诉我SQL Server文件的文件格式吗? - Can someone tell me the file format of SQL Server files? 有人可以告诉我为什么这个SQL查询不起作用 - Can someone tell me why this SQL query not working SQL 服务器地理数据类型 - 请告诉我为什么两个单独的多边形相交? 我没看到 - SQL Server Geography Data Type - Please tell me why two separate Polygons Intersect ? I don't see it RODBC-谁能告诉我为什么这不起作用? - RODBC-Can anyone tell me why this doesn't work? MS SQL Server:如何确定哪个字段导致Arifmetic错误 - MS SQL Server: how to determine which field is causing Arifmetic error 在SQL Server的表中添加一列。 导致查看错误 - In SQL Server, table add a column. causing view error 为什么此SQL查询导致“不明确的列名称”错误? - Why is this SQL Query causing an “Ambiguous column name” error? 谁能告诉我哪一个会在 SQL 中的数百万行数据中表现得更好 - Can anybody tell me which one will perform better with millions of rows of data in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM