@P_SqLCommand NVARCHAR(100) OUTPUT, --output which will be return
    @P_SqlList NVARCHAR(30),-- columns that needs to be filtered out
    @P_Table NVARCHAR(30),--Table Name needs to be use in the select statment
    @P_Input NVARCHAR(50),--the filter variable
    @P_Num varchar(5)
SET @P_Input='@P_Input'

    SET @P_SqLCommand='SELECT Top '+ @P_Num +' '+@P_SqlList+'  FROM '+
    @P_TABLE +' where '+ @P_SqlList + '=' + @P_Input;

give me an error:

Incorrect syntax near '='.

Any suggestions ?

I am using SQL Server. What am trying to do is to use stored procedure to for dynamic select statement and pass through by parameters, it is work fine till the where statement.

How I can treat @P_Input as character?


Whatever value of @P_SqlList the final WHERE sentence will be something like:


due you are setting it here:

SET @P_Input='@P_Input'

To solve this, add @P_Inputto the parameters list. Or, build the whole filter outside the dynamic query and pass it as a single value:

WHERE ' + @Filter

This form allow you to build a more complicated WHERE sentence.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.