1

I use sql to create the stored procedures , but I get an error " Incorrect syntax near ' = ' " And here is my code

CREATE PROC [dbo].[sp_T_CATEGORY_GetByTop]
@top nvarchar(10),
@where nvarchar(200),
@order nvarchar(200)
AS
    DECLARE @SQL AS NVARCHAR(500)
    SELECT @SQL = 'SELECT TOP ('+@top+') * FROM [T_CATEGORY]'
    IF LEN(@top) = 0
        BEGIN
            SELECT @SQL = 'SELECT * FROM T_CATEGORY'
        END
    IF LEN(@where) > 0
        BEGIN
            SELECT @SQL = @SQL + 'WHERE' + @where
        END
    IF LEN(@order) > 0
        BEGIN
            SELECT @SQL = @SQL + 'ORDER BY' + @order
        END
        EXEC(@SQL)

thanks everyone.

0

1 Answer 1

4

You need space before and after where and order by

IF LEN(@where) > 0
    BEGIN
        SELECT @SQL = @SQL + ' WHERE ' + @where
    END
IF LEN(@order) > 0
    BEGIN
        SELECT @SQL = @SQL + ' ORDER BY ' + @order
    END
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.