我有8位列,A1,A2,a3,a4,b1,b2,b3,b4。所有8个都是完全独立的,并且基于这些,应该填充另一个字段。
我想用文本A,B或AB更新其他字段,具体取决于8列中的哪一列设置为1。
以下是几个例子;
- 所有8个字段都设置为1,然后填充AB,
- 如果A3和B1设置为1,则填充AB,
- 如果A1和A3设置为1,则填充A,
- 如果B4和B2设置为1,则填充B。
因此,对于A1到B4的任何组合,应设置字段
以下是我所尝试的内容,但它不完整但会给出一个想法;
更新
来自adrianm的正确答案
UPDATE m
SET ref = ASet + BSet
FROM contactMaster m
inner join contact c on
m.contactid = c.contactid
CROSS APPLY (
SELECT CASE WHEN (c.A1 | c.A2 | c.A3 | c.A4) = 1 THEN 'C' ELSE '' END AS ASet
,CASE WHEN (c.B1 | c.B2 | c.B3 | c.B4) = 1 THEN 'D' ELSE '' END AS BSet
) AS CA1
where ref is null
答案 0 :(得分:2)
UPDATE ContactMaster
SET ref = ASet + BSet
FROM ContactMaster
INNER JOIN Contact
ON ContactMaster.ContactId = Contact.ContactId
CROSS APPLY (
SELECT CASE WHEN (Contact.A1 | Contact.A2 | Contact.A3 | Contact.A4) = 1 THEN 'A' ELSE '' END AS ASet
,CASE WHEN (Contact.B1 | Contact.B2 | Contact.B3 | Contact.B4) = 1 THEN 'B' ELSE '' END AS BSet
) AS CA1
WHERE ContactMaster.ref IS NULL
答案 1 :(得分:0)
IF (Select Count(*) from table where A1=1 AND A2 =1 AND a3 =1 AND a4 =1 AND b1 =1 AND b2 =1 AND b3 =1 AND b4=1 )>0
BEGIN
UPDATE MyTable
SET ColumnValue ='AB'
where A1=1 AND A2 =1 AND a3 =1 AND a4 =1 AND b1 =1 AND b2 =1 AND b3 =1 AND b4=1
END
ELSE IF (Select Count(*) from table where A1 =1 and A3 =1 )>0
BEGIN
Update MyTable set columnValue ='A'
where A1 =1 and A3 =1
END