鬱悶了,SQL SERVER為什麼報這個錯啊Incorrect syntax near

來源:互聯網
上載者:User

這個是我的預存程序

CREATE PROCEDURE [dbo].[Pagination]@Columns VARCHAR(500), -- The columns to be displayed, divide by comma@Tablename VARCHAR(100), -- The name of the table to be searched@OrderColumnName VARCHAR(100), -- The name of the column to be used in order@Order VARCHAR(50), -- The order method, ASC or DESC@Where VARCHAR(100), -- The where condition, if there is not conditon use 1=1@PageIndex INT, -- Current page index@PageSize INT, -- The size of the page@PageCount INT OUTPUT -- The total page count,define as output parameterASBEGINDECLARE @SqlRecordCount NVARCHAR(100) -- The SQL Statement to get the total count of the recordsDECLARE @SqlSelect NVARCHAR(1000) -- The SQL SELECT statmentSET @SqlRecordCount = N'SELECT @RecordCount = COUNT(*) FROM' + @Tablename + ' WHERE ' +@WhereDECLARE @RecordCount INTEXEC sp_executesql @SqlRecordCount, N'@RecordCount INT OUTPUT',@RecordCount OUTPUT -- Transfer the parameter dynamicIF(@RecordCount % @PageSize = 0)SET @PageCount = @RecordCount / @PageSizeELSESET @PageCount = @RecordCount / @PageSize + 1SET @SqlSelect = N'SELECT ' + @Columns + ' FROM(SELECT ROW_NUMBER() OVER (ORDER BY ' + @OrderColumnName    +' ' + @Order + ') AS tempid, * FROM '     + @Tablename + ' WHERE ' + @Where + ') AS tempTableName WHERE tempid    BETWEEN ' + STR((@PageIndex - 1)*@PageSize + 1) + ' AND ' + STR(@PageIndex * @PageSize)EXEC(@SqlSelect)END

 

運行下面這個的時候

DECLARE @PageCount intEXEC Pagination    'fname','employee','fname','DESC','1=1',1,20,@PageCount

 

SQL SERVER 報這個錯誤

Msg 102, Level 15, State 1, Line 1Incorrect syntax near 'FROMemployee'. (20 row(s) affected)

有Result 返回,但是為什麼報這麼一個錯誤啊。

 

Debug 了下

這個是動態產生的SQL語句

SELECT fname FROM(SELECT ROW_NUMBER() OVER (ORDER BY fname DESC) AS tempid, * FROM employee WHERE 1=1) AS tempTableName WHERE tempid    BETWEEN          1 AND         20

 

這句話我單獨在SQL SERVER分析查詢器裡,運行沒有錯誤,但是在預存程序裡為什麼有這個錯誤(⊙o⊙)?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.