如何在临时表中创建自动递增ID?

时间:2014-07-23 04:43:46

标签: sql-server

我在存储过程中创建了一个临时表:

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

3 个答案:

答案 0 :(得分:1)

尝试以下格式:

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

答案 1 :(得分:1)

CREATE TABLE #tbl(
    ID int IDENTITY(1,1),
    Correct bit
)

这将在您的表格中创建自动公司字段

答案 2 :(得分: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