易截截图软件、单文件、免安装、纯绿色、仅160KB

怎样用SQL语句判断一个数据表中至少N项不为空???

前两天一个学姐问我个SQL语句的问题,现在把解决方案贴出来,也算总结一下吧。
她的问题是:“一个表中有15个字段,用SQL语句判断其中5项或以上不为空,怎么判断,很急……”。
当时我很忙,没多看,呵呵,不够意思啦。但我请同事帮她写了个,以解燃眉之急,在此也谢谢小米。
今天再把QQ聊天记录翻出来,用本地数据库数据表试验了下, 有点问题,于是自己修改。
【详细情况,请参考SQL Server 联机丛书:索引“CASE‘;
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.zh-CHS/s10de_6tsql/html/658039ec-8dc2-4251-bc82-30ea23708cee.htm】

select distinct (case bulletinId when bulletinId then 1 else 0 end) +
(case title when title then 1 else 0 end) +
(case body when body then 1 else 0 end) +
(case bulletinType when bulletinType then 1 else 0 end) +
(case bulletinState when bulletinState then 1 else 0 end) +
(case companyId when companyId then 1 else 0 end) +
(case userId when userId then 1 else 0 end) +
(case createtime when createtime then 1 else 0 end) as '不为空字段数' from bulletin

当数据表bulletin中至少有一条记录时,也可以采取下面的方法。

1、简单 CASE 函数
select * from bulletin where
(case bulletinId when bulletinId then 1 else 0 end) +
(case title when title then 1 else 0 end) +
(case body when body then 1 else 0 end) +
(case bulletinType when bulletinType then 1 else 0 end) +
(case bulletinState when bulletinState then 1 else 0 end) +
(case companyId when companyId then 1 else 0 end) +
(case userId when userId then 1 else 0 end) +
(case createtime when createtime then 1 else 0 end) >= 5
2、CASE 搜索函数
select * from bulletin where
(case when bulletinId is not null or bulletinId!=''  then 1 else 0 end) +
(case when title is not null or title!='' then 1 else 0 end) +
(case when body is not null or body!='' then 1 else 0 end) +
(case when bulletinType is not null or bulletinType!='' then 1 else 0 end) +
(case when bulletinState is not null or bulletinState!='' then 1 else 0 end) +
(case when companyId is not


相关文档:

SQL Server 事务日志收缩小结

  对于每一个数据库来讲,都需要至少一个事务日志文件。事务日志文件是整个数据库的血液,如果没有事务日志的话,那么将无法进行任何操作。 
事务日志有什么东西?
  事务日志记录着在相关数据库上的操作,同时还存储数据库恢复(recovery)的相关信息。
 
  事务日志与数据库恢 ......

SQL语句大全(2)

在进行数据库操作时,无非就是添加、删除、修改,这得设计到一些常用的SQL语句,如下:
SQL常用命令使用方法:
(1) 数据记录筛选:
sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]"
sql="select * from 数据表 where 字段名 like %字段值% order by 字段名 [desc]"
sql="select top 10 * fro ......

拼接SQL造成的意想不到的后果

执行数据操作时,由于拼接SQL存在种种弊端,早就应该抛弃了,但在现实开发时,又由于种种原因,公司一直采用这种方式(UI层和逻辑层都有严格的过滤,倒也没出现过什么问题),但昨天开发时却出现了意想不到的问题,一个简单的语句会造成严重后果。简单的语句示例如下:
    /// <summary>
&nb ......

SQL函数库

1.字符串函数 :
datalength(Char_expr) 返回字符串包含字符数,但不包含后面的空格
length(expression,variable)指定字符串或变量名称的长度。
substring(expression,start,length) 不多说了,取子串
right(char_expr,int_expr) 返回字符串右边int_expr个字符
concat(str1,str2,...)返回来自于参数连结的字符串。dat ......

SQL Server2008 Resource Governor简介


Introduction to the SQL Server 2008 Resource Governor
Database Administration, Performance Tuning | January 4, 2010 | 4:05 pm
This is an excerpt from my free eBook, Brad’s Sure Guide to SQL Server 2008.
I think most of us are familiar with this situation: a SQL Server database is the bac ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号