+ -
当前位置:首页 → 问答吧 → 这个用触发器如何实现

这个用触发器如何实现

时间:2011-12-02

来源:互联网

有六个字段
CallIn Contact shangjijiaoban xianzhangdianhua Other shoujifangshi 
true false false false false 走访
true true false false false 走访|来访
true true false false true 走访|来访|其他


现在有第六个字段,shoujifangshi,当数据库插入或者更新后,这个字段的值为前面5个字段为true的对应值的累加,5个字段对应值分别为:走访、来访、电话联系、上级交办信访、县(市)长电话、其他,中间用“|”分开 。

作者: xulibing1016   发布时间: 2011-12-02

这个用计算列就行了,用不着触发器.

作者: qianjin036a   发布时间: 2011-12-02

计算式类似:
SQL code
shoujifangshi=isnull(case when callin=true then '走访' end+'|','')+
isnull(case when cantact=true then '来访' end+'|','')+
isnull(case when shangjijiaoban=true then '上级交办信访' end+'|','')+
isnull(case when xianzhangdianhua=true then '县(市)长电话' end+'|','')+
(case when other=true then '其他' else '' end)


你前面有五列,可是却给出了六项!

作者: qianjin036a   发布时间: 2011-12-02

SQL code

if object_id('tb','u') is not null
    drop table tb
go
create table tb
(
 CallIn bit,
 Contact bit,
 Phone bit,
 shangjijiaoban bit,
 xianzhangdianhua bit,
 other bit,
 shoujifangshi as stuff(case when CallIn=1 then '|走访' else '' end+
                  case when Contact=1 then '|来访' else '' end+
                  case when Contact=1 then '|电话联系' else '' end+
                  case when shangjijiaoban=1 then '|上级交办信访' else '' end+
                  case when xianzhangdianhua=1 then '|县(市)长电话' else '' end+
                  case when other=1 then '|其他' else '' end,1,1,'')
                  
                  
)
go
insert into tb(CallIn,Contact,Phone,shangjijiaoban,xianzhangdianhua,other)
select 'true','true','true','true','true','true'
select * from tb
/*
CallIn Contact Phone shangjijiaoban xianzhangdianhua other shoujifangshi
------ ------- ----- -------------- ---------------- ----- ----------------------------------------------------
1      1       1     1              1                1     走访|来访|电话联系|上级交办信访|县(市)长电话|其他

(1 行受影响)
*/

作者: pengxuan   发布时间: 2011-12-02

楼主少给了一列Phone,电话联系列

作者: pengxuan   发布时间: 2011-12-02

引用 3 楼 pengxuan 的回复:

SQL code

if object_id('tb','u') is not null
drop table tb
go
create table tb
(
CallIn bit,
Contact bit,
Phone bit,
shangjijiaoban bit,
xianzhangdianhua bit,
other bit,
shoujifangshi as s……

要改一下,把中间的那个表达式写成2楼的样子,你这样会出错的.
比如,当第一个为0的时候.前面会多一个"|"

作者: qianjin036a   发布时间: 2011-12-02

SQL code


declare @maintb table([1] int,[2] int, [3] int,[4] int, [5] int ,[6] int);
insert into @maintb select 1,0,1,1,0,0 union all
select 1,1,1,1,0,0;

declare @t table(id int,name varchar(100));
insert into @t select 1,'走访' union all
select 2,'来访' union all
select 3,'电话联系' union all
select 4,'上级交办信访' union all
select 5,'县(市)长电话' union all
select 6,'其它'

;with cte as (
select a.x,apy.name from  (
select ',-1'+case when [1]=1 then ',1' else '' end+ 
case when [2]=1 then ',2' else '' end+ 
case when [3]=1 then ',3' else '' end+ 
case when [4]=1 then ',4' else '' end+ 
case when [5]=1 then ',5' else '' end+ 
case when [6]=1 then ',6' else '' end+',' as x
from @maintb
) a outer apply(
 select name from @t b where CHARINDEX(','+cast(b.id as varchar(10))+',',a.x)>0 
) apy
)
select a.x,(select b.name+'|' from cte b where b.x=a.x for xml path('')) as xx from cte a group by a.x ;

/*
,-1,1,2,3,4,    走访|来访|电话联系|上级交办信访|
,-1,1,3,4,    走访|电话联系|上级交办信访|
*/
--x可以改成id来确定,这样才正确。

作者: jinfengyiye   发布时间: 2011-12-02