Why does SQL Server Report this error? incorrect syntax near

Source: Internet
Author: User

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 )?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.