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,
u.LastName,
…
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.
Please sign in to leave a comment.
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?
Or do you want to have a separate recognition prompt just for the @UserName variable?
Correct, I am declaring @UserEmail as a parameter to the query and then from the code that uses SqlCommand, I am adding a parameter with something like this:
and queryText equals to this:
If I do WHERE u.Email =:'@UserEmail', then I get the prompt for the parameter and after entering the parameter I get the error with about the `multi-part identifier` which looks like with this change, it is looking now for the UserEmail column:
=================================================================================
What did I expect?
I expected something like this:
Unfortunately, we can't pass value variables through parameters. In case of parameters, it can be only a value you enter from the prompt, but not a variable with a value you're passing through parameters.
I understand.
Thanks for the explanation.
Hey everyone,
I’m still a bit unclear on the conclusion reached here. Could someone explain why it’s not possible to run this type of query? It seems that JoyRider 's issue was more about the parameter not being parsed and recognized by Rider, rather than wanting to set it as a variable for dynamic recognition. Or perhaps I misunderstood the discussion.
I’m currently encountering the same issue with a straightforward query—Rider is not recognizing it as a parameter. Any insights would be greatly appreciated!
Thanks in advance.
PS. Settings > Tools > Database > Query Execution > User Parameters includes this pattern:
(?<![@<])@[a-zA-Z0-9_]+
https://youtrack.jetbrains.com/issue/DBE-20681/Alter-procedure-asks-for-parameters#focus=Comments-27-9808991.0-0