1

我想将表数据和文本框值插入到另一个表中。我正在尝试如下插入命令,但它不起作用。

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()"
4

2 回答 2

3

使用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()

的值UserQuizIDValue来自m.ToString()

于 2013-02-21T08:45:50.360 回答
0

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

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

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

于 2013-02-21T08:48:45.897 回答