从另一个表和值插入表

时间:2021-03-21 22:43:53

I want to insert table data and textbox value in to another table. I am trying insert command as below but it is not working.

我想将表数据和文本框值插入另一个表。我正在尝试插入命令如下,但它无法正常工作。

Insert into 
     Table_UserAnswer 
      (
         UserQuizID, 
         QuizID, 
         QuestionID, 
         Title, 
         Answer1, 
         Answer2, 
         Answer3, 
         Answer4,
         CorrectAnswer
       ) 
         '" + m.ToString() + 
      "', 
      select 
         top 5 QuizID, 
         QuestionID, 
         Title, 
         Answer1,
         Answer2,
         Answer3,
         Answer4,
         CorrectAnswer 
     from 
         [Table_Question] 
     order by 
          newid()"

2 个解决方案

#1


3  

use INSERT INTO...SELECT statement,

使用INSERT INTO ... SELECT语句,

INSERT INTO 
    Table_UserAnswer 
    (
      UserQuizID, 
      QuizID, 
      QuestionID, 
      Title, 
      Answer1, 
      Answer2, 
      Answer3, 
      Answer4, 
      CorrectAnswer)  
    SELECT TOP 5 
      'UserQuizIDValue', 
      QuizID, 
      QuestionID, 
      Title,
      Answer1,
      Answer2,
      Answer3,
      Answer4,
      CorrectAnswer 
    FROM   
      Table_Question
    ORDER BY 
      newid()

the value of UserQuizIDValue is from m.ToString().

UserQuizIDValue的值来自m.ToString()。

#2


0  

INSERT INTO TestTable (Col1, Col2) SELECT Col1, Col1 FROM Person.Contact

INSERT INTO TestTable(Col1,Col2)SELECT Col1,Col1 FROM Person.Contact

You have to make sure that number of columns in the Insert query should be same as the Select statement except if you have any identity column.

您必须确保Insert查询中的列数应与Select语句相同,除非您有任何标识列。

In your case "UserQuizID" is missing from the Select statement.

在您的情况下,Select语句中缺少“UserQuizID”。

#1


3  

use INSERT INTO...SELECT statement,

使用INSERT INTO ... SELECT语句,

INSERT INTO 
    Table_UserAnswer 
    (
      UserQuizID, 
      QuizID, 
      QuestionID, 
      Title, 
      Answer1, 
      Answer2, 
      Answer3, 
      Answer4, 
      CorrectAnswer)  
    SELECT TOP 5 
      'UserQuizIDValue', 
      QuizID, 
      QuestionID, 
      Title,
      Answer1,
      Answer2,
      Answer3,
      Answer4,
      CorrectAnswer 
    FROM   
      Table_Question
    ORDER BY 
      newid()

the value of UserQuizIDValue is from m.ToString().

UserQuizIDValue的值来自m.ToString()。

#2


0  

INSERT INTO TestTable (Col1, Col2) SELECT Col1, Col1 FROM Person.Contact

INSERT INTO TestTable(Col1,Col2)SELECT Col1,Col1 FROM Person.Contact

You have to make sure that number of columns in the Insert query should be same as the Select statement except if you have any identity column.

您必须确保Insert查询中的列数应与Select语句相同,除非您有任何标识列。

In your case "UserQuizID" is missing from the Select statement.

在您的情况下,Select语句中缺少“UserQuizID”。