当前位置: 代码迷 >> Sql Server >> 急用,求各位大侠帮助!解决方法
  详细解决方案

急用,求各位大侠帮助!解决方法

热度:56   发布时间:2016-04-27 12:50:19.0
急用,求各位大侠帮助!!!
数据库有两个字段 A B ,分别代表两种状态;  

现在要根实际情况,可以灵活的修改两个状态、、

假如A 有值,我把值赋给B ,假如B有值,我把值赋给A, 两个都没值,我就默认赋给A


怎样写存储过程呢??



------解决方案--------------------
case when .. else .. end
------解决方案--------------------
if ... else ....
------解决方案--------------------
SQL code
goif OBJECT_ID('tbl')is not nulldrop table tblgocreate table tbl(A varchar(3),B varchar(3))goinsert tblselect 'aaa',null union allselect null,'bbb' union allselect null,null --declare @str varchar(3)set @str='bbb'update tblset A=case when A is null then @str      when A is not null and B is null then A else A end,    B=case when A is null then B      when A is not null and B is null then @STR else B endselect * from tbl/*A    Baaa    bbbbbb    bbbbbb    NULL*/
------解决方案--------------------
SQL code
goif OBJECT_ID('tbl')is not nulldrop table tblgocreate table tbl(A varchar(3),B varchar(3))goinsert tblselect 'aaa',null union allselect null,'bbb' union allselect null,null --测试declare @str varchar(3)set @str='new'update tblset A=case when A is null then @str      when A is not null and B is null then A else A end,    B=case when A is null then B      when A is not null and B is null then @STR else B endselect * from tbl/*A    B-------------aaa    newnew    bbbnew    NULL*/    这样看得明显点,注意对比两个字段的值
  相关解决方案