Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using quoted list with in()

Explorer ,
Jun 12, 2007 Jun 12, 2007
I can't get this query to return results unless I manually put in a list of zip codes ('80918','80917','80920','80902','80922','80907','80909','80919')

When I have the same list fed in as a variable SQL adds extra quotes to the list and I get no results. What do I need to do to prevent this?

declare
@ZipCodes varchar(300)


SELECT
StoreName,
Address,
City,
StateAbbreviation,
PostalCode,
PhoneNumber,
WebAddress
FROM
CatholicStores
JOIN
States
ON
State_Pk = State_Fk
WHERE
PostalCode IN (@zipcodes)
ORDER BY
PostalCode,
StoreName
TOPICS
Database access
850
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jun 13, 2007 Jun 13, 2007
cfqueryparam handles quoting for you. remove all single quotes in: #attributes.zipcodelist# and use

<cfqueryparam value="#attributes.zipcodelist#" cfsqltype="CF_SQL_VARCHAR" list="Yes">
Translate
LEGEND ,
Jun 13, 2007 Jun 13, 2007
Two things to try are ListQualify() or cfqueryparam list="yes"
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 13, 2007 Jun 13, 2007
athanasiusrc,

If you have a non-quoted list of values, try using cfqueryparam. Its untested but should be something like this

<cfset theList ="80918,80917,80920,80902,80922,80907,80909,80919">
<cfif listLen(theList) eq 0>
<cfset theList = listAppend(theList, 0)>
</cfif>

<cfquery ...>
SELECT
StoreName,
Address,
City,
StateAbbreviation,
PostalCode,
PhoneNumber,
WebAddress
FROM
CatholicStores
JOIN
States
ON
State_Pk = State_Fk
WHERE
PostalCode IN
(
<cfqueryparam value="#theList#" cfsqltype="cf_sql_varchar" list="true">
)
ORDER BY
PostalCode,
StoreName
</cfquery>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 13, 2007 Jun 13, 2007
Using list yes in the query param and switching between quoted and nonquoted don't fix the problem.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 13, 2007 Jun 13, 2007
Are you using dynamic sql? If not post the exact query you tried.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 13, 2007 Jun 13, 2007
I have also tried running it as a stored procedure.


<cfquery name="getlocations" Datasource="#application.ds#" >
SELECT
StoreName,
Address,
City,
StateAbbreviation,
PostalCode,
PhoneNumber,
WebAddress
FROM
CatholicStores
JOIN
States
ON
State_Pk = State_Fk
WHERE
PostalCode IN (<cfqueryparam value="#'preservesinglequotes(attributes.zipcodelist)'#" cfsqltype="CF_SQL_VARCHAR" list="Yes">)
ORDER BY
PostalCode,
StoreName
</cfquery>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 13, 2007 Jun 13, 2007
cfqueryparam handles quoting for you. remove all single quotes in: #attributes.zipcodelist# and use

<cfqueryparam value="#attributes.zipcodelist#" cfsqltype="CF_SQL_VARCHAR" list="Yes">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 13, 2007 Jun 13, 2007
LATEST
That did it!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources