急用,求各位大侠帮助!!!
数据库有两个字段 A B ,分别代表两种状态;
现在要根实际情况,可以灵活的修改两个状态、、
假如A 有值,我把值赋给B ,假如B有值,我把值赋给A, 两个都没值,我就默认赋给A
怎样写存储过程呢??
[解决办法]
case when .. else .. end
[解决办法]
if ... else ....
[解决办法]
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*/
[解决办法]
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*/ 这样看得明显点,注意对比两个字段的值