将参数传递给“Row_Number”

时间:2014-02-06 19:37:19

标签: stored-procedures sql-server-2012 row-number

当我尝试参数化列名时,我使用以下查询获得以下 syntax error 。的 Incorrect syntax near '@SortBy'.

我能够使用动态SQL使其工作,但问题是 Entity Framework 不会为 SPs with exec 创建复杂类型。

是否有办法/解决方法使此查询不是动态SQL。

DECLARE
        @MLSIdList varchar(max),
    @SortBy varchar(max),
    @LowRow int,
    @HighRow int

SET @MLSIdList = '12-632393,12-73464,12-39834,12-78237'
SET @SortBy = 'ORDER BY a.mlsnum, a.yb, a.RatePlugMID  '
SET @LowRow = 11
SET @HighRow = 23


SELECT a.mlsId , a.ADDRESS, a.APN , a.RatePlugMID,a.hod , a.yb , a.UNUM ,
       a.CHANGETYPE , a.CITY , a.County,  ROW_NUMBER() OVER ( @SortBy ) AS RowNumber,
FROM mls_unified_svo_tbl a (nolock) 
WHERE a.MLSNUM IN ( @MLSIdList ) 

0 个答案:

没有答案