简体   繁体   English

如何摆脱SQL Server 2008表中的NULL值

[英]How to get rid of NULL values in SQL Server 2008 table

I have a table like: 我有一张桌子:

1   0.22    0.14    0.05
23  11      0.32    0.16
4   NULL    1       0.39
NULL NULL   .8      1
NULL .5     .7      NULL 

I want to modify the table to become 我想修改表成为

1   0.22  0.14  0.05
23   1    0.32  0.16
4   -1      1   0.39
-1  -1     .8     1
-1  .5     .7    -1

If you look I changed the NULL by -1 如果你看我把NULL改为-1

I was trying: 我在努力:

SELECT
  coalesce([a1], -1), coalesce([a2], -1),coalesce([a3], -1), coalesce([a4], -1)
FROM tableee;

Which is correct, but when I do a SELECT * I get the table with null values... 哪个是正确的,但是当我做一个SELECT *我得到了具有null值的表...

How do I modify the table so when I do a SELECT * I do not get NULL but -1 instead? 我如何修改表,所以当我执行SELECT *我不会得到NULL而是-1而不是?

If you want to change the values in the table you need to update the table: 如果要更改表中的值,则需要更新表:

UPDATE Tableeee t
SET t.a1 = COALESCE(t.a1,-1),
    t.a2 = COALESCE(t.a2,-1),
    t.a3 = COALESCE(t.a3,-1)
    t.a4 = COALESCE(t.a4,-1)

Or you can change the columns to not nullable and give a default value of -1. 或者,您可以将列更改为不可为空,并提供默认值-1。 You will probably want to do this if you have that requirement. 如果您有这个要求,您可能希望这样做。 Updating the data would just be a bandaide, you need to enforce this requirement in the DB. 更新数据只是一个绷带,您需要在数据库中强制执行此要求。

You could change the table schema so that the columns are non-nullable, and have a default value of -1. 您可以更改表模式,以使列不可为空,并且默认值为-1。 That should alter the data so that all of the NULL values become -1. 这应该改变数据,以便所有NULL值变为-1。

If you don't want to actually change the data but only the data set returned by the query, consider creating a view based off of your COALESCE() ing query. 如果您不想实际更改数据而只想更改查询返回的数据集,请考虑根据COALESCE()查询创建视图。 Then you can use that view whenever you want to see -1 in place of NULL. 然后,只要您想要看到-1代替NULL,就可以使用该视图。

There is no way to actually modify the table so that you get -1 instead of null with a select *. 没有办法实际修改表,因此使用select *得到-1而不是null。 You are allowing nulls for those columns so it is not desirable logic to hide the nulls. 您允许这些列使用空值,因此隐藏空值不是理想的逻辑。

Now that being said, you have three options: 现在说,你有三个选择:

1) Update the table to not allow nulls in those columns and then set the default to -1 (or use a trigger) note: This requires admin priviledges, which you may not have . 1)更新表以不允许这些列中的空值,然后将默认值设置为-1(或使用触发器) 注意:这需要管理员权限,您可能没有
2) Always do a select with your coalease or an isnull() test 2)始终使用coalease或isnull()测试进行选择
3) You could make a view of the same table and then in the view select do your coalesce() or isnull() logic, then you could just select * from the view, and it would give you the same table but with the null changed out. 3)你可以查看同一个表,然后在视图中选择你的coalesce()或isnull()逻辑,然后你可以从视图中选择*,它会给你相同的表,但是null改变了。

Just FYI, option 2 or 3 has overhead, so only do this if #1 is not an option. 仅供参考,选项2或3有开销,所以只有在#1不是选项时才这样做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM