Running C# SQL String with a parameter causes [S0002][137] Must declare the scalar variable

I have a query inside of a C# class like this:

public static class Queries
    public static class Assets
        public const string ByUserId= @"
            SELECT u.FirstName,
            FROM Users
            WHERE UserId = @UserId";

I can have the cursor inside of the query string, and press ctrl+enter to execute the query right from the class. If it has no parameters, like @UserId then the results will be displayed without any issues. However when there is a parameter then it will show the error message [S0002][137] Must declare the scalar variable “@UserId”.

Is there any way to set the variable which will be forwarded to the query and make it possible to execute it? Sure, I can copy the query to the console and declare the variable, but I am wondering if there is a way to pass the variable to the query from the declared string in the C# class.




Based on the error message, it looks like that variable is beyond the scope when it's being executed. May I know how and in which code portion you initialized / declared your variable? Also, need to know from which portion you want to pass the parameter value.

Sorry, I have not been clear enough. I am talking only about “design” time, when I am using Rider IDE, not when I am running the code.

in Rider when a C# class is open, I can have a mouse cursor inside of a SQL string declaration, then press ctrl + enter and it will run the query and display the results in the Services. However, if I have a parameter defined in the SQL query string, it will not run since it does not know the the parameter value. The question is if there is a place in Rider where I can define this parameter so Rider, could look it up when running the SQL query from the string under cursor.


We have user parameters section in File | Settings | Database | Query Execution | User Parameters 

You can trigger a parameter prompt: where user = :'id'. You can add the value from the prompt


Thanks for your reply.

I tried it and I cannot make it work. My parameter name is @UserEmail and here is what it looks like.

I even added the parameter pattern “@Name” but it did not seem to help. The parameters prompt does not recognize that there is a parameter.

What am I doing wrong?


Please sign in to leave a comment.