user1661868
user1661868

Reputation: 31

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

Upvotes: 1

Views: 1399

Answers (2)

John Woo
John Woo

Reputation: 263883

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

Upvotes: 3

alok_dida
alok_dida

Reputation: 1733

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.

Upvotes: 0

Related Questions