在单个更新语句中更新多个记录

时间:2012-04-23 03:01:28

标签: sql sql-server tsql

过去几个小时我一直在这里,最后我会寻求帮助。我正在尝试使用另一个表中的多个记录更新一个表中的多个不同记录。下面是设置:

表x(QuestID,SectionID,Questxt)PK = QuestID

表y(QuestAGID,QuestID,AGID)PK = QuestAGID

对于某些情况,一个表有问题,另一个表有问题答案。问题与部分相符。每个问题答案最多匹配一个问题。通过复制问题答案表中的记录,我现在在一个问题的问题答案表中有2个相同的记录。我希望能够获取那些重复的记录,并用我创建的新问题替换问题标识符。我无法想出一个能够在多个记录上一对一替换值的更新语句。

这是我到目前为止所提出的:

declare @a as int
select @a = Count(x.sectionid) from x
where Sectionid=6584

update y
set QuestId = (select QuestId, ROW_NUMBER() 
    OVER (ORDER BY questid) AS Row2
    from x 
    where SectionId=7700)
from y
join x on y.QuestId=x.QuestId
where x.Questid IN 
( 
select top (@a) y.QuestId, ROW_NUMBER() 
    OVER (ORDER BY y.Questid) as Row1
    from y
inner join x
on y.QuestId=x.QuestId
where x.SectionId=6584
)

非常感谢任何帮助。

谢谢!

编辑:以下是两个子查询的结果示例:

查询1

select QuestionId, ROW_NUMBER() 
    OVER (ORDER BY questionid)
    from Questions 
    where SectionId=7700

结果:

QuestionId     column2    
-------------  ---------- 
31254          1          
31255          2  

查询2

declare @a as int
select @a = Count(Questions.sectionid) from Questions
where Sectionid=6584

select top (@a) Questions_AnswerGroup.Questionid, ROW_NUMBER() 
OVER (ORDER BY Questions_AnswerGroup.Questionid) 
from Questions_AnswerGroup
inner join Questions
on Questions_AnswerGroup.QuestionId=Questions.QuestionId
where Questions.SectionId=6584

结果:

Questionid     column2    
-------------  ---------- 
24990          1          
24991          2  

2ND编辑:我会尽可能地澄清这一点,以帮助每个人安心,并感谢大家一起来看看。

所以这是我的第一步,我根据章节编号重复了Question_AnswerGroup记录。

insert into Questions_AnswerGroup (Questions.QuestionId,
Questions_AnswerGroup.AnswerGroupId,Questions_AnswerGroup.ControlTypeId,
         Questions_AnswerGroup.IsRequired,Questions_AnswerGroup.Rank,Questions_AnswerGroup.ColumnCou    nt,
Questions_AnswerGroup.EmailRecipient,Questions_AnswerGroup.FilterName,Questions_AnswerGroup    .ResponseName,
Questions_AnswerGroup.IsHeader,Questions_AnswerGroup.IsVariable,Questions_AnswerGroup.MaxVa    lue,
Questions_AnswerGroup.DefaultValue,Questions_AnswerGroup.CharCount,Questions_AnswerGroup.Ad    ditionalInfo,
Questions_AnswerGroup.ConditionsId,Questions_AnswerGroup.InitVisible)
SELECT Questions_AnswerGroup.QuestionId,Questions_AnswerGroup.AnswerGroupId,
Questions_AnswerGroup.ControlTypeId,Questions_AnswerGroup.IsRequired,Questions_AnswerGroup.    Rank,
Questions_AnswerGroup.ColumnCount,Questions_AnswerGroup.EmailRecipient,Questions_AnswerGrou    p.FilterName,
Questions_AnswerGroup.ResponseName,Questions_AnswerGroup.IsHeader,Questions_AnswerGroup.IsV    ariable,
Questions_AnswerGroup.MaxValue,Questions_AnswerGroup.DefaultValue,Questions_AnswerGroup.Cha    rCount,
Questions_AnswerGroup.AdditionalInfo,Questions_AnswerGroup.ConditionsId,Questions_AnswerGro    up.InitVisible
FROM Questions_AnswerGroup
inner join Questions
on Questions_AnswerGroup.QuestionId=Questions.QuestionId
where Questions.SectionId=6584

这给了我一个在Question_AnswerGroup表中看起来像这样的输出:

QuestionAnswerGroupId   QuestionId
17438                      24990
17439                      24991
17444                      24990
17445                      24991

目前,QuestionAnswerGroupIds(orginial和我刚复制的)都与第6584节中的QuestionId相关联。我现在需要将我创建的两个QuestionAnswerGroupIds与第7700节中的两个问题相关联没有答案。

Select questionid from Questions where sectionid=7700

结果如下:

questionid
31254
31255

因此,在Conculsion中,我需要采用问题表中存在的两个questionID(来自第7700节)并将它们放在我在上面创建的副本的位置。

编辑3:这是重复问题的创建方式:

insert into Questions(Questions.Rank,Questions.QuestionText,Questions.IsActive,Sections.SectionId,Questions.ShowNumber,Questions.IsTextOnly,Questions.ContextHelp,Questions.ConditionsId,Questions.InitVisible)
select Questions.Rank,Questions.QuestionText,Questions.IsActive,(Max(Sections.SectionId)),Questions.ShowNumber,Questions.IsTextOnly,Questions.ContextHelp,Questions.ConditionsId,Questions.InitVisible
from Questions, Sections
where Questions.SectionId=6584
Group by      Questions.Rank,Questions.QuestionText,Questions.IsActive,Questions.ShowNumber,Questions.IsTextOnly,Questions.ContextHelp,Questions.ConditionsId,Questions.InitVisible

1 个答案:

答案 0 :(得分:0)

一个猜测,但我认为关于你所追求的东西还有一些未解决的问题。

;WITH src AS 
(
  SELECT QuestionId, r = ROW_NUMBER() OVER (ORDER BY QuestionId)
  FROM dbo.Questions
  WHERE SectionId = 7700
), trg AS
(
  SELECT QuestionId, r = ROW_NUMBER() OVER (ORDER BY QuestionId)
  FROM dbo.Questions
  WHERE SectionId = 6584
)
UPDATE trg
  SET QuestionId = src.QuestionId
  FROM trg INNER JOIN src
  ON trg.r = src.r;