2013-04-10 37 views
0

在我的数据库中,我有7行3个不同的实体,我想根据ID值将一个实体的内容镜像到另外两个实体。我不知道更新声明是否合适。SQL Server基于ID复制内容

CoId DocumentType StatusId StatusDescription Default Text Progression Environment RequiredOnAssign TS DocumentFilterGroup 

这是我的列标题,CoId可以具有以下三个值之一,1,2,或3。我希望基于状态ID的1的内容复制到2和3。除此之外,我还遇到了麻烦。

+2

给例如请:你有什么样的数据,你的结果需要什么 – shibormot 2013-04-10 20:59:43

回答

1

如果我理解正确的话,那么自联接是你最好的朋友:

UPDATE t1 
SET DocumentType = t1.DocumentType, StatusDescription = t1.StatusDescription, Default = t1.Default -- the same for the rest of the fields 
FROM table t1 
INNER JOIN table t2 
ON t1.CoID in (2,3) and t2.CoID = 1 
WHERE StatusID = ...