简体   繁体   中英

insert into table from another table and values

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

use INSERT INTO...SELECT statement,

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() .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM