This is my Stored Procedure
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/@ pagesizeElse Set @ Pagecount = @ recordcount/@ pagesize + 1 Set @ Sqlselect = N 'Select' + @ Columns + 'From (select row_number () over (order' + @ Ordercolumnname + '' + @ Order + ') As tempid, * from' + @ Tablename + 'Where' + @ Where + ') As temptablename where tempid' + STR (@ pageindex-1) * @ pagesize + 1) + And' + STR (@ pageindex * @ pagesize)Exec (@ Sqlselect) End
Run the following command:
Declare@ PagecountIntExecPagination'Fname','Employee','Fname','Desc','1 = 1', 1, 20, @ pagecount
This error is reported by SQL Server.
MSG 102,Level15,State1, line 1 incorrect syntax near'Fromemployee'. (20Row(S) affected)
A result is returned, but why is this error reported.
Debug
This is a dynamically generated SQL statement.
SelectFnameFrom(SelectRow_number ()Over(Order ByFnameDesc)AsTempid ,*FromEmployeeWhere1 = 1)AsTemptablenameWhereTempidBetween1And20
In this statement, I run the SQL Server analyzer without errors, but why is this error in the stored procedure )?