1

过去几个小时我一直在这,我终于想寻求帮助了。我正在尝试用另一个表中的多个记录更新一个表中的多个不同记录。设置如下:

表 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  

第二次编辑:我会尽可能地澄清这一点,以帮助大家理解并感谢大家的关注。

所以这是我的第一步,我根据节号复制了 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

目前,两个 QuestionAnswerGroupId(原始的和我刚刚复制的)都与属于第 6584 节的 QuestionId 相关联。我现在需要获取我创建的两个 QuestionAnswerGroupId,并将它们与第 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
4

1 回答 1

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;
于 2012-04-23T03:53:04.267 回答