SQL 更新 UPDATE得条件 从 SELECT 查询的结果 同一表
UPDATEC_RouteSETRouteFlg=@NoFlgWHERE(RouteFlg=@YesFlgANDCustomerID=@CustomerIDANDStar...
UPDATE C_Route
SET RouteFlg = @NoFlg
WHERE (RouteFlg = @YesFlg AND CustomerID = @CustomerID AND
StartClientID = @StartClientID AND EndClientID = @EndClientID AND
RouteID ! = @RouteID)
正常情况除了RouteID其他几个参数都可以根据RouteID在查询一次C_Route表得到得,但是这样就执行了两次sql,我想直接在更新事就得到这些参数,应该怎么写sql 展开
SET RouteFlg = @NoFlg
WHERE (RouteFlg = @YesFlg AND CustomerID = @CustomerID AND
StartClientID = @StartClientID AND EndClientID = @EndClientID AND
RouteID ! = @RouteID)
正常情况除了RouteID其他几个参数都可以根据RouteID在查询一次C_Route表得到得,但是这样就执行了两次sql,我想直接在更新事就得到这些参数,应该怎么写sql 展开
2个回答
2012-12-21
展开全部
UPDATE
C_Route test_to
SET
test_to.RouteFlg = test_from.NoFlg
FROM
C_Route test_to JOIN C_Route test_from
ON (
test_to.RouteFlg = test_from.YesFlg
AND test_to.CustomerID = test_from.CustomerID
AND test_to.StartClientID = test_from.StartClientID
AND test_to.EndClientID = test_from.EndClientID
AND test_to.RouteID <> test_from.RouteID
)
WHERE
test_from.RouteID = @RouteID;
是这个效果么?
C_Route test_to
SET
test_to.RouteFlg = test_from.NoFlg
FROM
C_Route test_to JOIN C_Route test_from
ON (
test_to.RouteFlg = test_from.YesFlg
AND test_to.CustomerID = test_from.CustomerID
AND test_to.StartClientID = test_from.StartClientID
AND test_to.EndClientID = test_from.EndClientID
AND test_to.RouteID <> test_from.RouteID
)
WHERE
test_from.RouteID = @RouteID;
是这个效果么?
追问
恩恩,应该是,我试试,我刚才写的把条件放错地方了,改成你这样得应该能行,我在试试
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询