这个用触发器如何实现
时间: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
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
作者: pengxuan 发布时间: 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 s……
要改一下,把中间的那个表达式写成2楼的样子,你这样会出错的.
比如,当第一个为0的时候.前面会多一个"|"
作者: qianjin036a 发布时间: 2011-12-02
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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28