简体   繁体   中英

Use parameter as column in where in SQL Server stored procedure

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

This is the query and its giving this error during run time

Incorrect syntax near '2'.
Invalid usage of the option NEXT in the FETCH statement.

which means query getting wrong after

WHERE P.Name LIKE %'+@Search+'%

you don't need dynamic sql for this just test for the value of the filter value in combination with the column in your WHERE expression:

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

If you really want to use dynamic SQL instead of Executing it SELECT @SQLQuery and then look for the syntax issues by copying to another query window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM