2013-02-21 175 views
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()" 
+0

m.ToString()的用戶是什麼? – 2013-02-21 08:49:55

+0

m = Dr [0] .ToString(); max = Convert.ToInt16(m); max = max + 1; } con.Close(); con.Dispose(); Dr.RoseClose(); Dr.Dispose(); cmd.Cancel(); cmd.Dispose(); m = max.ToString(); – user1661868 2013-02-21 09:04:47

回答

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

0

INSERT INTO TestTable的(Col1中,col2的) SELECT Col1中,Col1中 FROM Person.Contact

你必須確保在插入查詢列的數量應該是相同的,只是如果你有Select語句任何標識欄。

在你的情況下,Select語句中缺少「UserQuizID」。

+0

if(Dr.Read()) { m = Dr [0] .ToString(); max = Convert.ToInt16(m); max = max + 1; } con.Close(); con.Dispose(); Dr.RoseClose(); Dr.Dispose(); cmd.Cancel(); cmd.Dispose(); m = max.ToString(); – user1661868 2013-02-21 09:08:05

+0

你用它做什麼?是否爲UserQuizId?如果它不是主鍵? – 2013-02-21 09:15:52

相關問題