2013-06-10 3 views
1

LIKE를 삽입하고 coldfusion을 사용하여 쿼리에서 my 매개 변수 내에 와일드 카드를 사용하는 데 약간의 문제가 있습니다.cfqueryparam에서 와일드 카드 사용 - Coldfusion

을 -

<cfquery name="sample" datasource="database"><!---Take the query here---> 
    SELECT * <!---select all---> 
    FROM table <!---from the table "table" 
    WHERE 
    <cfloop from="1" to="#listLen(selectList1)#" index="i"> 
     #ListGetAt(selectList1, i)# = <cfqueryparam cfsqltype="cf_sql_varchar" value="#ListGetAt(selectList2,i)#" /> <!--- 
                search column name = query parameter 

                using the same index in both lists 
                (selectList1) (selectList2) ---> 
    <cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but 
               the very last element of the list (in that 
               case we don't need an "AND"---> 
    </cfloop> 
</cfquery> 

내 목표는 내가 매개 변수 같은를 입력 할 수 있습니다 곳으로 코드를 얻을 수 있습니다 (오류는 쿼리를 실행할 수 없습니다 읽기하지만 현재 작동하지 않습니다) : 여기에 현재 작동하는 것입니다

<cfquery name="sample" datasource="database"><!---Take the query here---> 
    SELECT * <!---select all---> 
    FROM table <!---from the table "table" 
    WHERE 
    <cfloop from="1" to="#listLen(selectList1)#" index="i"> 
     #ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" /> <!--- 
                search column name = query parameter 

                using the same index in both lists 
                (selectList1) (selectList2) ---> 
    <cfif i neq listLen(selectList1)>AND</cfif> <!---append an "AND" if we are on any but 
               the very last element of the list (in that 
               case we don't need an "AND"---> 
    </cfloop> 
</cfquery> 
+0

디버깅을 켜고 생성 된 쿼리를 복사 한 다음 쿼리 분석기에서 실행하는 것이 좋습니다. 나는 보통 나의 모든 실수를 그런 식으로 발견 할 수있다. –

+0

그 구문이 작동해야합니다. 나는 MS SQL Server와 동일한 것을 사용한다. 목록 항목 중 하나가 비어있어 쿼리에 오류가 발생 했습니까? –

+0

@ Miguel-F No. 코드 섹션 # 1에서 쿼리가 나타나지만 코드 섹션 # 2에서는 나타나지 않습니다. – Zac

답변

0

구문이 정상적으로 보입니다. 필자는 Microsoft SQL Server를 실행하는 일부 코드에서 동일한 종류의 논리를 사용합니다.

코드를 디버그하고 생성되는 SQL을 보려면이 방법을 사용해보십시오.

<!--- Comment out your query tag to debug </cfquery> --->

<cfoutput> <!--- add a cfoutput tag to "see" your generated code ---> 
SELECT * 
FROM table 
WHERE 
<cfloop from="1" to="#listLen(selectList1)#" index="i"> 
    #ListGetAt(selectList1, i)# LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#ListGetAt(selectList2,i)#%" /> 
    <cfif i neq listLen(selectList1)>AND</cfif> 
</cfloop> 
</cfoutput> <!--- add a cfoutput tag to "see" your generated code ---> 

<!--- Comment out your query tag to debug <cfquery name="sample" datasource="database"> --->

은 대부분 쿼리에 의존하는 코드의 나머지 부분을 깰 것입니다. 나는 보통 <cfabort> 태그를 닫고 </cfoutput> 태그를 붙이면 처리가 중지되고 SQL이 표시됩니다.

이 목적은 생성 된 SQL 코드를 브라우저에 출력하는 것입니다. 그것을 살펴보고 오류를 발견 할 수 있는지 확인하십시오. 생성 된 SQL을 복사하여 쿼리 도구 (쿼리 분석기)에 붙여넣고 테스트 할 수도 있습니다.