ant
August 9, 2020, 9:12am
1
Struggling with the variables in a custom query.
When I use ONE variable (:P1) it works fine, but as soon as I include another one (:P2) it does not provide a result.
To illustrate, this is my query:
The above works well. But as soon as I replace UserID = 1 with UserID = :P2, it doesn’t work.
I searched the forum but couldn’t find any answers. What am I doing wrong?
George
August 9, 2020, 10:22am
2
What server model and database are you using?
ant
August 9, 2020, 12:38pm
3
Good question, how can I find this info? - sorry I am still kinda new to this stuff…
George
August 9, 2020, 1:31pm
5
So you have included the extra variable P2 in your list of variables, but I don’t see it used in your custom query?
Maybe you forgot to replace the value 1 with :P2
ant
August 9, 2020, 1:39pm
6
That is exactly the issue, once I replace value 1 with :P2 the query doesn’t work anymore.
Then, when I only use the :P2 and delete the ModuleID = :P1, it works…
George
August 9, 2020, 2:02pm
7
This is probably because the value of your identity is empty, like when the user is not logged in yet.
ant
August 9, 2020, 7:24pm
8
Thanks for the suggestion, but unfortunately still no luck. Also, shouldn’t the “test value” override the “expression” when pressing the play button (execute query) in this window?
Do you get an error or just an empty resultset?
ant
August 13, 2020, 2:55pm
10
@patrick sorry for delay - I get an empty result.
Do you know if the adminsecurity.identity
expression returns the correct id? Perhaps it is null
or an other unexpected value, the query will then return an empty result since no records match the condition.
ant
August 19, 2020, 10:05am
12
Thanks @patrick for the suggestion but I don’t think that is the problem.
This is my table:
When I use :P1 only, it works:
When I add “ModuleID” and define it, it works too. So far so good.
However, when I replace the ModuleID with a variable :P2 (=“objectives”), it stops working:
patrick
August 19, 2020, 10:09am
13
Here 2 thing you could try
replace :P1
and :P2
with a ?
.
use different parameter names like :userid
and :moduleid
.
ant
November 26, 2020, 4:17pm
14
Hi @patrick - thanks for your suggestions and apologies it took a while to get into this.
Using different parameters doesn’t make a difference.
Replacing with ? works! What is the logic behind this? Any reading I can do to better understand?
Struggling with this issue for a while over various projects, so some further insight on how to address this would be very much welcomed.
? Parameters are “positional” and the colon parameters are “named”. You can google named and positional parameters for whichever db server you are using to learn more.
ant
November 27, 2020, 2:57pm
16
Thanks, that makes sense. Still wondering why named variables is giving me issues. Could this be a database server issue?