2014-07-23 79 views
0

我在存儲過程中創建一個臨時表:如何在臨時表中創建自動遞增Id?

CREATE TABLE #tbl (
     [Id]   INT, 
     [Correct] BIT  
     ) 

我怎麼能填充此表的自動生成越來越多的Id和這樣的數據:

SELECT Correct FROM question 
INNER JOIN answer 
ON (question.questionId = answer.questionId) 
WHERE question.questionid = 25 
ORDER BY answer.answerId 

回答

1

嘗試採用以下格式:

CREATE TABLE #tbl (
     [Id]   INT IDENTITY(1,1), 
     [Correct] BIT  
     ) 
1
CREATE TABLE #tbl(
    ID int IDENTITY(1,1), 
    Correct bit 
) 

這將在您的表中創建auto inc字段

1

沒有IDENTITY你可以試試這個

SELECT  ROW_NUMBER() OVER (ORDER BY answer.answerId) AS Id, Correct 
FROM  question INNER JOIN 
      answer ON (question.questionId = answer.questionId) 
WHERE  question.questionid = 25 
ORDER BY answer.answerId