msg 120,级别15,状态1,过程generate\u exame,第6行,insert语句的select列表包含的项少于insert列表

vs3odd8k  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(284)

我想插进去 question 包含这些列的表

C#_T_F_Id, C#_T_F_Q, C#_T_F_Choices, C#_Mcq_Id, C#_MCQ_Q, C#_Choices

执行后 Generate_Exame 程序我应该怎么做:

create procedure Generate_Exame 
    @course_id int
as 
    if @course_id = 600
    begin
        insert into [dbo].[Question](C#_T_F_Id, C#_T_F_Q, C#_T_F_Choices,
                                     C#_Mcq_Id, C#_MCQ_Q, C#_Choices)
            select * 
            from
                (select top(3) 
                     T.C#_T_F_Id, T.C#_T_F_Q, T.C#_T_F_Choices
                 from 
                     C#_T_F T
                 order by
                     newid()) as t1
            union all
            select * 
            from
                (select top(7) 
                     C.C#_Mcq_Id C#_Q_id, C.C#_MCQ_Q C#_question, C.C#_Choices Choices              
                 from 
                     C#_MCQ C
                 order by
                     newid()) as t2)
    end
dsf9zpds

dsf9zpds1#

如果我理解的很清楚,你想:
从组合结果集中向表中插入数据。
并排合并两个结果集。第一个提供列1、2和3,而第二个提供列4、5和6。
除此之外,两个结果集(左侧和右侧)的长度也不相同。一个有3行,另一个有7行。我想这些数字可能会有所不同。
左边的行和右边的行没有固定的顺序。您通过使用随机uuid排序来生成它们,这样每次运行查询时都可以更改。
为了做到这一点,你需要在每一边产生一个行号。然后一个简单的完全联接将合并两个结果集。
例如:

insert into [dbo].[Question] (
  C#_T_F_Id, C#_T_F_Q, C#_T_F_Choices,
  C#_Mcq_Id, C#_MCQ_Q, C#_Choices
)
select -- Step #4: produce combined rows, ready for insert
  a.T.C#_T_F_Id, a.T.C#_T_F_Q, a.T.C#_T_F_Choices,
  b.C#_Q_id, b.C#_question, b.Choices
from ( -- Step #1: Produce the left result set with row number (rn)
  select *, row_number() over(order by ord) as rn
  from (
    select top(3)
      T.C#_T_F_Id, T.C#_T_F_Q, T.C#_T_F_Choices, 
      newid() as ord
    from C#_T_F T 
    order by ord
  ) x
) a 
full join ( -- Step #2: Produce the right result set with row number (rn)
  select *, row_number() over(order by ord) as rn
  from (
    select top(7) 
      C.C#_Mcq_Id C#_Q_id, C.C#_MCQ_Q C#_question, C.C#_Choices Choices,
      newid() as ord
    from C#_MCQ C
    order by ord
  ) y
) b on a.rn = b.rn -- Step #3: Full join both result sets by row number (rn)
vtwuwzda

vtwuwzda2#

你有六个专栏 INSERT 条款。但是,联合查询中只有3列。

-- You are inserting 6 columns
insert into [dbo].[Question](C#_T_F_Id, C#_T_F_Q, C#_T_F_Choices,
                                     C#_Mcq_Id, C#_MCQ_Q, C#_Choices)

-- You are selecting only 3 columns.
            select * 
            from
                (select top(3) 
                     T.C#_T_F_Id, T.C#_T_F_Q, T.C#_T_F_Choices
                 from 
                     C#_T_F T
                 order by
                     newid()) as t1
            union all
            select * 
            from
                (select top(7) 
                     C.C#_Mcq_Id C#_Q_id, C.C#_MCQ_Q C#_question, C.C#_Choices Choices              
                 from 
                     C#_MCQ C
                 order by
                     newid()) as t2)

如果需要有6列,则需要根据连接条件以某种方式连接这两个select语句。

相关问题