怎样用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
相关文档:
1. 消除trigger的嵌套调用。最好不要用 EXEC sp_configure 'nested triggers', '0', 应该在trigger中使用判断语句, 例如:if not update (name) return。
2. 使用 not for replication 禁止在复制的时候触发trigger。
3. 创建publisher article的时候, 设置 copy user triggers为 true。
这样保证:trigger不会嵌套调 ......
create or replace procedure c
(
v_deptno in emp.deptno%type,
v_max out emp.sal%type
)
as
begin
select max(sal+nvl(comm,0)) into v_max from emp where deptno=v_deptno;
end;
create or replace procedure cc
(
v_empno in emp.empno%type,
v_sal out emp.sal%type,
v_comm out emp.comm% ......
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 ......
SQL SERVER的分类汇总
SQL SERVER中使用GROUP BY对数据进行分类汇总,我们也可以使用WITH ROLLUP和WITH CUBE配合GROUP BY进行“增强”了的分类汇总,那么他们两个是如何增强GROUP BY的汇总能力的呢?
一.功能增强
1.使用WITH ROLLUP
用下面的例子说明,GROUP使用了3个分组字段:GROUP BY A, B, C WITH ROLLUP ......