如何在更新命令中选择命令

时间:2011-06-20 18:32:05

标签: sql-server select

我有三张桌子

table t1;
Sid sValue
1   abc
2   bcd

table t2
Sid  Pid  Mid
1    a    9
2    a    10
3    b    9

table t3
Mid  MValue
9    ZZZZ
10   yyyy

我想更新表't1'和 set t1.sVal =“”其中t2.Pid ='a'和t3.MValue ='zzzz'

我该怎么做请帮助我提前谢谢

我试过像

update t1 set sVal="" where Sid=(select Sid from t2 where Pid='a' and Mid=(select Mid from t3 where MVale='ZZZZ'))

但它不起作用并且出现了错误 像

An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name. 

2 个答案:

答案 0 :(得分:2)

问题是你在set语句中使用双引号而不是单引号。

设置sVal =''

未设置sVal =“”

但是如果你想要一个更干净的更新语句,这段代码应该这样做。

update t1 set sVal=''
from t1
inner join t2 on t1.sid=t2.sid
inner join t3 on t2.Mid=t3.Mid
where t2.Pid='a' and t3.MVale='ZZZZ'

答案 1 :(得分:0)

试试这个:

update t1 SET t1.sVal = ''
INNER JOIN t2
ON t1.sid = t2.sid
INNER JOIN t3
ON t2.mid = t3.mid
WHERE t2.Pid='a' and t3.MValue='zzzz'