SQL FROM与INTO #table无法正常工作

时间:2015-06-02 12:18:50

标签: sql-server tsql

我正在扫除我的SQL(或者在这种情况下是TSQL),并且一直在编写存储过程。到目前为止,在我的代码中,我可以使用LINQPad进行良好的执行,因为我测试了我的代码。但是,这个特定的块,我将每个用户的最高分数添加到临时表#QuizScores中,提供了一般语法错误

  

关键字'SELECT'

附近的语法不正确

我试图移动这个陈述的所有部分,但似乎失败了。您可能分享的任何见解?

以下是声明:

SELECT * 
INTO #QuizScores 
FROM (SELECT ROW_NUMBER() 
        OVER (
            PARTITION BY UserID 
            ORDER BY count(#QuizResponses.IsCorrectAnswer) DESC) AS 
            RowNumber, 
            UserID, 
            count(#QuizResponses.IsCorrectAnswer) AS 
            Points 
            FROM #QuizResponses) 
            JOIN #QuizResponses 
            ON #QuizResponses.QuizAttemptID = #QuizAttemptList.QuizAttemptID;

2 个答案:

答案 0 :(得分:1)

[编辑:这反映了该问题的早期修订。]

来自MSDN

  

SELECT ... INTO在默认文件组中创建

强调补充。

答案 1 :(得分:1)

我已根据您的要求将一些假设数据插入临时表和修改后的语法

IF OBJECT_ID('tempdb..#QuizResponses') IS NOT NULL 
DROP TABLE #QuizResponses
GO
IF OBJECT_ID('tempdb..#QuizScores') IS NOT NULL 
DROP TABLE #QuizScores
CREATE TABLE #QuizResponses (UserId int,correctanswer int,IsCorrectAnswer varchar(1),QuizAttemptID INT)
INSERT INTO #QuizResponses (UserId,correctanswer,IsCorrectAnswer)values (1,1,'Y',2),(2,1,'N',2)

select P.RowNumber,P.Points,P.UserId INTO #QuizScores from (
SELECT ROW_NUMBER() OVER (PARTITION BY a.UserID 
ORDER BY count(a.IsCorrectAnswer) DESC) AS RowNumber, a.UserID , 
count(a.IsCorrectAnswer) AS Points 
FROM #QuizResponses a
                JOIN #QuizResponses S ON S.UserId = A.UserId
                group by a.UserId )P