在SQL Server存储过程中使用参数作为列

时间:2017-11-02 18:31:03

标签: c# sql-server stored-procedures stored-functions

def printName():
    String = ''
    for row in range(0,8):    
        for column in range(0,30):
            if (column == 0 or row == 0 or column == 29):
                String =String + '*'
            elif (column == 2 or ((row == 1 or row == 4) and column > 2 and column < 6) or (column == 6 and row != 1 and row < 4) or (column == row - 1 and row > 3)):  
                String = String + 'R'
            elif (column == 11 or (row == 1 and column > 8 and column <14) or ( row == 7 and column > 8 and column <14 )):  
                String =String+'I'
            elif (((row == 1 or row == 4 or row == 7) and column > 16 and column < 20) or (column == 16 and (row == 2 or row == 3 or row == 7)) or (column == 20 and (row == 1 or row == 5 or row == 6))):  
                String = String + 'S'
            elif (column == 23 or column == 27 or row ==4 and (column>23 and column <27)):
                String = String + 'H'
            else:      
                String = String + ' '    
        String = String + '\n'
    return String


name = printName()
#now the string is in the variable name, outside of the function's scope.
print(name)

这是查询及其在运行时发出此错误

  

'2'附近的语法不正确。
  FETCH语句中NEXT选项的使用无效。

表示在

之后查询出错
BEGIN
    DECLARE @SQLQuery AS NVARCHAR(MAX)

    IF(@Search IS NOT NULL)
    BEGIN
        DECLARE @dyColumn sysname ;

        IF(@Filter = 'IsNew')
        BEGIN
            SET @dyColumn = 'IsNew'
        END
        ELSE IF(@Filter = 'IsOnSale')
        BEGIN
            SET @dyColumn = 'IsOnSale'
        END
        ELSE IF(@Filter = 'IsFeatured')
        BEGIN
            SET @dyColumn = 'IsFeatured'
        END

        SET @SQLQuery = 'SELECT P.*, C.Id AS CategoryId, C.Name AS CategoryName, C.Logo AS CategoryLogo,
                    CO.Id AS CompanyId, CO.Name AS CompanyName, CO.Logo AS CompanyLogo, COUNT(*) OVER() TotalCount
                    FROM Products P
                    JOIN Categories C ON  P.CategoryId = C.Id
                    JOIN Companies CO ON  P.CompanyId = CO.Id 
                    WHERE P.Name LIKE %'+@Search+'% AND '+@dyColumn+' = true
                    ORDER BY P.Name
                    OFFSET '+CAST(@PageSize AS nvarchar(100))+'*('+CAST(@PageNumber AS nvarchar(100)) +'- 1) ROWS
                    FETCH NEXT '+CAST(@PageSize AS nvarchar(100))+'ROWS ONLY OPTION (RECOMPILE);'

        EXECUTE(@SQLQuery)
    END

1 个答案:

答案 0 :(得分:0)

你不需要动态sql来测试过滤器值的值,并结合WHERE表达式中的列:

SELECT
    P.*
    ,C.Id AS CategoryId
    ,C.Name AS CategoryName
    ,C.Logo AS CategoryLogo
    ,CO.Id AS CompanyId
    ,CO.Name AS CompanyName
    ,CO.Logo AS CompanyLogo
    ,COUNT(*) OVER() TotalCount
FROM
    Products P
    JOIN Categories C
    ON  P.CategoryId = C.Id
    JOIN Companies CO
    ON  P.CompanyId = CO.Id 
WHERE
    @Search IS NOT NULL
    AND P.Name LIKE '%' + @Search + '%'
    AND ( 
       (@Filter= 'IsNew' AND IsNew = 1)
       OR (@Filter= 'IsOnSale' AND IsOnSale = 1)
       OR (@Filter= 'IsFeatured' AND IsFeatured = 1)
       OR (@Filter NOT IN ('IsNew','IsOnSale','IsFeatured'))
    )
ORDER BY
    P.Name
OFFSET  (@PageSize)*(@PageNumber)- 1 ROWS
FETCH NEXT (@PageSize) ROWS ONLY OPTION (RECOMPILE);

如果您真的想使用动态SQL而不是执行它SELECT @SQLQuery,然后通过复制到另一个查询窗口来查找语法问题。