郁闷了,SQL SERVER为什么报这个错啊Incorrect syntax near

这个是我的存储过程

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 parameter
AS
BEGIN
DECLARE @SqlRecordCount NVARCHAR(100) -- The SQL Statement to get the total count of the records
DECLARE @SqlSelect NVARCHAR(1000) -- The SQL SELECT statment
SET @SqlRecordCount = N'SELECT @RecordCount = COUNT(*) FROM' + @Tablename + ' WHERE ' +@Where
DECLARE @RecordCount INT
EXEC sp_executesql @SqlRecordCount, N'@RecordCount INT OUTPUT',@RecordCount OUTPUT -- Transfer the parameter dynamic

IF(@RecordCount % @PageSize = 0)
SET @PageCount = @RecordCount / @PageSize
ELSE
SET @PageCount = @RecordCount / @PageSize + 1

SET @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 int
EXEC Pagination    'fname','employee','fname','DESC','1=1',1,20,@PageCount

 

SQL SERVER 报这个错误

Msg 102, Level 15, State 1, Line 1
Incorrect 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⊙)?

posted @ 2010-07-26 11:30  类型安全的心  阅读(19258)  评论(3编辑  收藏  举报