【问题标题】:Msg 120, Level 15, State 1, Procedure Generate_Exame, Line 6,The select list for the INSERT statement contains fewer items than the insert listMsg 120, Level 15, State 1, Procedure Generate_Exame, Line 6, INSERT 语句的选择列表包含的项少于插入列表
【发布时间】:2020-11-18 11:32:55
【问题描述】:

我想在包含这些列的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

【问题讨论】:

  • 错误信息很清楚。您声明要插入 6 列,但您的 select 仅返回 5 列。
  • 再读查询你会发现Select返回6列` TC#_T_F_Id ,TC#_T_F_Q , TC#_T_F_Choices,CC#_Mcq_Id C#_Q_id , CC#_MCQ_QC#_question ,CC# _Choices 选择`
  • Union 不会将列添加在一起,而是将行添加在一起,因此现在通过您的编辑,您尝试将 3 列插入 6 列。
  • 我想添加这些列 ``` TC#_T_F_Id ,TC#_T_F_Q , TC#_T_F_Choices,CC#_Mcq_Id,C#_Q_id , CC#_MCQ_Q,C#_question ,CC#_Choices ```到 question 表,其中包含在联合之后我应该写什么查询的列
  • 听起来您想加入 2 个随机行表(而不是合并它们)。然而,最好的办法是展示一些示例数据和预期结果——这是清楚地解释 SQL 问题的最简单方法。

标签: sql asp.net .net sql-server database


【解决方案1】:

如果我理解得很好,你想:

  • 将数据从组合结果集中插入到表中。
  • 并排合并两个结果集。第一个提供第 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)

【讨论】:

  • 我编辑我的查询再次阅读它将解释我的工作,我想要的是在union too 之后添加列
【解决方案2】:

INSERT 子句中有六列。但是,UNION 查询中只有 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 列,则需要根据 JOIN 条件以某种方式连接两个 SELECT 语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多