我做了存储过程,但我不知道在WHERE子句上放什么来过滤空列

时间:2016-07-17 11:08:53

标签: sql-server vb.net stored-procedures

我在存储过程中创建了一个INNER JOIN,但是我不知道要把什么放到我的WHERE子句中来过滤那些具有空值的列,并且只显示那些在特定列上没有null的行。

CREATE PROCEDURE [dbo].[25]
@param1 int
AS
    SELECT c.Name, c.Age, c2.Name, c2.Country
    FROM Cus C 
    INNER JOIN Cus2 C2 ON c.id = c2.id
    WHERE c2.country is not null and c2.id = @param1
    Order by c2.Country 
RETURN 0

ID 1

+-----+----+---------+---------+
| QID | ID |  Name   | Country |
+-----+----+---------+---------+
|   1 |  1 | Null    | PH      |
|   2 |  1 | Null    | CN      |
|   3 |  1 | Japhet  | USA     |
|   4 |  1 | Abegail | UK      |
|   5 |  1 | Norlee  | Ger     |
+-----+----+---------+---------+

ID 2

+-----+----+----------+---------+
| QID | ID |   Name   | Country |
+-----+----+----------+---------+
|   1 |  2 | Null     | PH      |
|   2 |  2 | Null     | CN      |
|   3 |  2 | Reynaldo | USA     |
|   4 |  2 | Abegail  | UK      |
|   5 |  2 | Norlee   | Ger     |
+-----+----+----------+---------+

ID 3

+-----+----+----------+---------+
| QID | ID |   Name   | Country |
+-----+----+----------+---------+
|   1 |  3 | Gab      | PH      |
|   2 |  3 | Null     | CN      |
|   3 |  3 | Reynaldo | USA     |
|   4 |  3 | Abegail  | UK      |
|   5 |  3 | Norlee   | Ger     |
+-----+----+----------+---------+

我希望当我选择C表中的任何用户时,它将显示C子表数据并删除空名称行,并保留非空名称列的行。

期望的结果:

C表(父母)

+----+---------+-----+
| ID |  Name   | Age |
+----+---------+-----+
|  3 | Abegail |  31 |
+----+---------+-----+

C2表(儿童)

+-----+----+----------+---------+
| QID | ID |   Name   | Country |
+-----+----+----------+---------+
|   1 |  3 | Gab      | PH      |
|   3 |  3 | Reynaldo | USA     |
|   4 |  3 | Abegail  | UK      |
|   5 |  3 | Norlee   | Ger     |
+-----+----+----------+---------+

2 个答案:

答案 0 :(得分:4)

WHERE column IS NOT NULL是过滤掉NULL值的语法。

答案 1 :(得分:0)

解决方案1:测试非空值

示例:

   WHERE yourcolumn IS NOT NULL 

解决方案2:在你的where子句中测试comparaison值(comparaison substract null values)

示例:

   WHERE yourcolumn = value 
   WHERE yourcolumn <> value
   WHERE yourcolumn in ( value)
   WHERE yourcolumn not in ( value) 
   WHERE yourcolumn between value1 and value2
   WHERE yourcolumn not between value1 and value2