English 中文(简体)
如何查询列中包含 < 无法读取数据 > 的行?
原标题:How to query for rows containing <Unable to read data> in a column?

我有一个 SQL 表格,其中在 SQL 服务器管理器中查看某些列时含有 < code@ lt; 无法读取数据 & gt; 。 有人知道如何查询 < code_ lt; 无法读取数据 & gt; ? 我可以用 < code> 更新表格设置的列 = NULL 修改此列中的数据, 显示密钥= 值 的位置, 但我如何找到是否有其他行存在这种错误的数据?

最佳回答

我建议不要替换数据。 它没有问题, 只是 SSMs 无法在编辑面板 < / em> 中正确显示数据。 数据库本身的数据从您的描述来看是完全正常的 。

此脚本显示问题 :

create table test (id int not null identity(1,1) primary key, 
    large_value numeric(38,0));
go

insert into test (large_value) values (1);
insert into test (large_value) values (12345678901234567890123456789012345678);
insert into test (large_value) values (1234567890123456789012345678901234567);
insert into test (large_value) values (123456789012345678901234567890123456);
insert into test (large_value) values (12345678901234567890123456789012345);
insert into test (large_value) values (1234567890123456789012345678901234);
insert into test (large_value) values (123456789012345678901234567890123);
insert into test (large_value) values (12345678901234567890123456789012);
insert into test (large_value) values (1234567890123456789012345678901);
insert into test (large_value) values (123456789012345678901234567890);
insert into test (large_value) values (12345678901234567890123456789);
insert into test (large_value) values (NULL);
go

select * from test;
go

SELECT 工作将正常, 但是在对象探索器中显示 编辑前200行将不会 :

"https://i.sstatic.net/pw17P.png" alt="无法编辑Data"/ >

",http://connect.microsoft.com/SQLServer/feedback/details/356/sms-edit-first-200-rows-sms-es-unable-unable-to-read-data-for-deminal-p-s-s-s-s-while-p-s-s-s-s-p-s-p-s-s-p-s-serf-times-timal-p-p-s-s-serl=“noreferrer”。2012 SSMS 2012 仍然存在同样的问题。

如果我们看", http://msdn.microsoft.com/en-us/library/aa258832%28v=sql.80%29.aspx" rel=“noreferr”>Numeric和Decimal 的细节,我们就会看到问题发生在一个奇特的边界,精确度为29,实际上是 not SQL服务器边界(精度28是):

Precision   Storage bytes
1 - 9   5
10-19   9
20-28   13
29-38   17

如果我们检查了.Net (SSMS是一个管理式应用程序), < a href="http://msdn.microsoft.com/en-us/library/364x0z75.aspx" rel="noreferr" >十进制精确表 ,我们就能很快看到问题的关键所在是:精密度 < 坚固> 28-29 重要数字 。 因此, Net < code> decitimal 类型无法映射高精度( & gt;29) SQL 服务器 number //decode > dectimal 类型。

这将不仅影响SSMS 显示, 而且还影响您的应用程序。 SISIS 等专门应用程序将使用高精度表示法, 如 < code> DT_ NUMERC < /code > :

DT_NUMERIC An exact numeric value with a fixed precision and scale. This data type is a 16-byte unsigned integer with a separate sign, a scale of 0 - 38, and a maximum precision of 38.

现在回到问题: 您可以通过简单的查看值来发现无效的条目。 知道 C# 表示范围可以包含大约值( 7.9 x 10 < sup>28 到 7.9 x 10 < sup>28 ) / ( 10 < sup> 0 到 28 ) / (这个范围取决于比例) ` (这个范围取决于比例) 您可以在每列中搜索范围以外的值( 要在列中搜索的实际值取决于列的尺度)。 但这引出了用什么来取代数据的问题?

我建议使用专门的进出口工具,即能够处理高精度数字值的工具。SSIS是显而易见的候选者。但即使微小的bcp.exe 也符合该法案。

如果你的数值实际上不正确(如:真正的腐败),那么我建议运行 < a href="http://msdn.microsoft.com/en-us/library/ms174338.aspx" rel="noreferr"\\code>DBCC CHECKTable(.)与DATA_Purity 运行:

<强>DATA_实用

Causes DBCC CHECKDB to check the database for column values that are not valid or out-of-range. For example, DBCC CHECKDB detects columns with date and time values that are larger than or less than the acceptable range for the datetime data type; or decimal or approximate-numeric data type columns with scale or precision values that are not valid.

For databases created in SQL Server 2005 and later, column-value integrity checks are enabled by default and do not require the DATA_PURITY option. For databases upgraded from earlier versions of SQL Server, column-value checks are not enabled by default until DBCC CHECKDB WITH DATA_PURITY has been run error free on the database. After this, DBCC CHECKDB checks column-value integrity by default.

问题: datetime 列中如何出现这个问题?

use tempdb;
go

create table test(d datetime)

insert into test (d) values (getdate())

select %%physloc%%, * from test;

-- Row is on page  0x9100000001000000

dbcc traceon(3604,-1);

dbcc page(2,1,145,3);

Memory Dump @0x000000003FA1A060
0000000000000000:   10000c00 75f9ff00 6aa00000 010000             ....uùÿ.j .....
Slot 0 Column 1 Offset 0x4 Length 8 Length (physical) 8

dbcc writepage(2,1,145, 100, 8, 0xFFFFFFFFFFFFFFFF)

"https://i.sstatic.net/YIYfe.png" alt="顶部200"/>

dbcc checktable( test ) with data_purity;

Msg 2570, Level 16, State 3, Line 2 Page (1:145), slot 0 in object ID 837578022, index ID 0, partition ID 2882303763115671552, alloc unit ID 2882303763120062464 (type "In-row data"). Column "d" value is out of range for data type "datetime". Update column to a legal value.

问题回答

如上文所建议,这些错误通常发生在精确度和比例不保存时。 如果您对 SISIS 感到舒适, 那么您就可以拿到那些腐败的行。 了解马丁·史密斯创建的数值 。

CREATE TABLE T(ID int ,C DECIMAL(38,0));
INSERT INTO T VALUES(1,9999999999999999999999999999999999999) 

以上表格复制错误 。 这里的第一列代表主密钥 。 我插入了大约1000行左右, 其中很少是损坏的值 。 下面是 SSIS 软件包设计 。

"https://i.sstatic.net/XtbT8.gif" alt="SSIS设计"/ >

在 Data 转换中,i 将出现错误并试图将其投影到十进制( 38, 0) 的 C 列中。由于转换或脱轨错误会发生,因此我将错误行重新定位为 OLEDB 命令,该命令基本上更新了表格,并将列设置到 NULL

Update T
Set C=NULL
where ID=?

C 和 ID 的值将被定向为 oledb 命令 。如果没有错误, 则im 插入表格( 实际上不需要这样做 ) 。 如果您在表格中有一个主键列, 这将有效 。

如果日期时间列有错误, 可以写入 sql 查询以校验日期时间值的格式 。 请通过 < a href=" http:// msdn. microsoft. com/ en- us/library/ ms 1873477. aspx" rel= “ nofollow norefererr" >MSDN 链接 的有效日期时间值

   Select * from YourTable where ISDATE(Col)!=1

我认为您可以用光标获取数据。 请用光标查询( 如下面的查询) 重试一次 :

DECLARE VerifyCursor CURSOR FOR
SELECT *
FROM MyTable
WHILE 1=1 BEGIN
    BEGIN Try
        FETCH FIRST FROM VerifyCursor INTO @Column1, @Column2, ...

        INSERT INTO @MyTable2(Column1, Column2,...)
        VALUES (@Column1, @Column2, ...)
    END TRY
    BEGIN CATCH
    END CATCH
    IF (@@FETCH_STATUS<>0) BREAK
End
OPEN VerifyCursor
CLOSE VerifyCursor
DEALLOCATE VerifyCursor

替换错误数据很简单,

UPDATE table SET column = NULL WHERE key_column =  Some value 




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签