读书人

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

发布时间: 2012-05-21 18:04:41 作者: rapoo

急用,求各位大侠帮助!!!
数据库有两个字段 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*/    这样看得明显点,注意对比两个字段的值 

读书人网 >SQL Server

热点推荐