zoukankan      html  css  js  c++  java
  • 郁闷了,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⊙)?

  • 相关阅读:
    sublime text 4 vim 插件配置
    ssh-keygen 的使用
    distribution transaction solution
    bilibili 大数据 视频下载 you-get
    Deepin 20.2.1 安装 MS SQL 2019 容器版本
    【转】使用Linux下Docker部署MSSQL并加载主机目录下的数据库
    【转】You Can Now Use OneDrive in Linux Natively Thanks to Insync
    dotnet 诊断工具安装命令
    Linux 使用 xrandr 设置屏幕分辨率
    【转】CentOS 7.9 2009 ISO 官方原版镜像下载
  • 原文地址:https://www.cnblogs.com/herbert/p/1785118.html
Copyright © 2011-2022 走看看