Why can't I use the same cfqueryparam in two locations in the same query when using CFScript?
I'm not sure if I am doing something wrong, but it seems that you can not use a cfqueryparam more than once in a single query if you write it in CFScript.
This behavior is not consistent with CFML. I just re-wrote a similar query from CFML to CFScript and I'm getting the following error: cfsqlparam 'id' is not defined
local.query = new Query();
local.query.setSql("
SELECT id
FROM myTable
WHERE myTable.id = :id OR myTable.parentId 开发者_Python百科= :id
");
local.query.addParam(name="id", cfsqltype="CF_SQL_INTEGER", value=arguments.id, maxlength=10);
local.query.execute().getResult();
If I take out the OR myTable.parentId = :id
it works perfectly well. Do I have to create a param for every location I intend to use one?
If it was a CFQuery you would have one cfqueryparam for each value:
where myTable.id = <cfqueryparam... value="#arguments.id#" />
or myTable.parentid = <cfqueryparam ... value="#arguments.id#" />
so I'm guessing you need to do the same thing in script:
local.query.setSql("
SELECT id
FROM myTable
WHERE myTable.id = :id OR myTable.parentId = :pid ");
local.query.addParam(name="id", cfsqltype="CF_SQL_INTEGER",value=arguments.id,maxlength=10);
local.query.addParam(name="pid",cfsqltype="CF_SQL_INTEGER",value=arguments.id,maxlength=10);
As for the question "why can't the one addParam address both placeholders" I'm guessing it comes down to how the query is parsed by ColdFusion - because you specify two param placeholders the parser probably expects to find two params defined.
精彩评论