需要有关更新SQL Server查询的帮助(在存储过程中)

时间:2011-07-22 15:31:49

标签: sql stored-procedures

我对使用存储过程非常非常新,我的任务是更新一个。在存储过程中,我需要更新以下查询:

update si_systemdetail  
set status = 'Production'  
where systemname in (select distinct systemname 
                     from @systemHistory 
                     where status = 'Production')`

我需要将其更新为ProductionProduction,而不是将状态更新为Production w/o Application@systemHistory是一个由xml数据填充的临时表,还有一个StatusHistory表,其中包含状态Production w/o Application所需的值。

基本上,当其他表中的Production相应地等于Production w/o Applicationsystemname时,我需要将状态设置为ProductionProduction w/o Application

我是否可以在一个声明中执行此操作,或者我是否需要发表两个声明,一个用于说明Production,另一个用于说明Production w/o Application

如果您需要更多信息,如果我没有充分解释,请告诉我。我真的想学习如何做到这一点。

TIA, 汤姆

2 个答案:

答案 0 :(得分:0)

这需要在两个语句中完成,除非有一些键来关联三个表(si_systemdetail,@ systemHistory,@ SystemName)。如果有一个字段你可以加入这三个表(即.... Guid或某种ResourceID),那么你可以在一个中完成。

有关系:

UPDATE sysd
SET status = 
CASE
    WHEN sh.SystemName OR stah.SystemName = 'Production' THEN 'Production'
    WHEN stath.SystemName = 'Production w/o Application' THEN 'Production w/o Application'
   ELSE [Default_Value_Here]
 END
FROM si_systemdetail sysd
 LEFT JOIN (
    SELECT DISTINCT [key],systemname 
            FROM @systemHistory 
            WHERE status = 'Production'
   ) sh
    ON sysd.[Key] = [sh.Key]
 LEFT JOIN (
    SELECT DISTINCT [key],systemname 
            FROM @StatusHistory 
            WHERE status IN ('Production', 'Production w/o Application')
   ) stath
    ON sysd.[Key] = stath.[Key]

没有关系:

update si_systemdetail  
set status = 'Production'  
where systemname IN (select distinct systemname 
                 from @systemHistory 
                 where status = 'Production')`

update si_systemdetail  
set status = 
CASE
   WHEN status = 'Production w/o Application' THEN 'Production w/o Application'
  ELSE 'Production'
END
WHERE systemname IN (select distinct systemname 
                 from @statusHistory 
                 where status IN ('Production', 'Production w/o Application')

答案 1 :(得分:0)

我认为我让它变得比它需要的更难。

`update si_systemdetail
设置状态='生产' 其中systemname in(从@systemHistory中选择不同的系统名称,其中newstatus ='Production')
更新si_systemdetail set status ='生产无应用' 其中systemname in(从@systemHistory中选择不同的系统名称,其中newstatus ='生产无应用')

if(@@ error<> 0) 开始 回滚事务 返回1 end`