Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

Proper Query syntax using a comma delimited list

I doing a cfquery against a table with a field call "gla". I want to use a listing (session.qmlist) generated from a prior input screen to compare against the table field "gla". If I get a hit from the table against the listing, I want to output the results.

I tried using Valuelist, and a  cfloop to go to the 1-to-many elements in the input listing, and I am not getting a full listing back.


<cfset session.qmlist = #form.qmlist#>
            
 <!---               <cfloop From = "1" To = "#ListLen(session.qmlist)#" index = "Counter">  --->
      
            <cfquery name="qMAgla" datasource="#REQUEST.DSN.Source#">
            select distinct upper(substring(gla,1,4)) gla
            from a_smc
            where upper(substring(gla,1,4))       IN ('#session.qmlist#')
            and date_clsd = '01/01/1900'
            </cfquery>
<!---               </cfloop>       
       --->
            <cfdump var="#qMAgla.gla#">


0
mahpog
Asked:
mahpog
  • 5
  • 2
  • 2
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you cannot do that "directly". what database are you working with?
0
 
mahpogAuthor Commented:
I am using sybase.
0
 
azadisaryevCommented:
what's the data type of your GLA field and the values in your SESSION.QMLIST? integers? varchar?
can you post sample of data contained in your session.qmlist variable?

meanwhile:
try using
where upper(substring(gla,1,4)) IN (<cfqueryparam value="#session.qmlist#" list="yes">)

instead of your
where upper(substring(gla,1,4))       IN ('#session.qmlist#')

alternatively, IN (#quotedvaluelist(session.qmlist)#) may also do the trick

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
mahpogAuthor Commented:
Using your snytax or what I have I only get back one 'GLA', when I should get a few more from the original qmlist having successful hits against the sybase table.

the first line following is the qmlist.
the second line is the one "GLA" that goes to the output from the query.

B6DP,E8T3,F177,FA2L,FE4T,FE7W,FE94,FG5H,FN24,FR2C,FR3K,FR40,FR9G,FRHT,FRWT,L34G
B6DP query - <======  query result = qMAgla.gla

Should be:  
B6DP,F177,FE94


 
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I don't know if CF somehow allows it, but the simple:
  IN ('#session.qmlist#')

will not work, as the list will be interpreted as single string value.

you would have to change every <,> in the value of gmlist by <','>, that would work. but I am no CF developer, so I don't know an easy way in CF to do so.
0
 
azadisaryevCommented:
wait a minute: do you mean to say tat your GLA column contains comma-delimited list(s)???
please tell me no!!!
if your answer is yes, i have heard good things about a book "Database design for mere mortals"
0
 
gdemariaCommented:

As pointed out above, the string you need in the    IN (here)   clause, needs to look like this...
..   in ('B6DP','E8T3','F177','FA2L','FE4T')

To do this in coldfusion, you need to change the delimiter of the list first.

<cfset newList = listChangeDelims(session.qmlist, "','")

here is that last parameter in 'slow motion'...    

      "  '  ,   '   "     (remove the spaces of course)

Now newList will contain..

   B6DP','E8T3','F177','FA2L','FE4T

But notice there are no starting or ending quotes.   So be sure to add those in the sql statement..

 where upper(substring(gla,1,4)) IN ('#newList#')

if you look at your debug output and see that the quotes have been escaped, you can add this function...

 where upper(substring(gla,1,4)) IN ('#preserveSingleQuotes(newList)#')

0
 
mahpogAuthor Commented:
gdemaria,

I did your suggestion and as you can see it creates the "IN" properly and there are 7 records that match.

but my output from qMAgla.gla only outputs the first hit and not the additonal 6 other records????

(I added the group by and order by thinking that perhaps that would cause all records to come out, but it only outputs the first hit: B6DP.  I should get the next 6 which includes F177..




Open in new window

qMAgla (Datasource=RQITest, Time=203ms, Records=7) in  D:\webserve\mahrqi\smcdev\home\smcage1.cfm @ 09:15:18.018

		select distinct upper(substring(gla,1,4)) gla
		from a_smc 
		where upper(substring(gla,1,4))	IN ('B6DP','E8T3','F177','FA2L','FE4T','FE7W','FE94','FG5H','FN24','FR2C','FR3K','FR40','FR9G','FRHT','FRWT','L34G')
		and date_clsd = '01/01/1900'
                group by upper(substring(gla,1,4))
                order by upper(substring(gla,1,4))


		

		

Open in new window

0
 
gdemariaCommented:
You're not showing the code where you try to output the results.  THat would help...

But there are two basic ways, either loop use CFLOOP or CFOUPUT

<cfoutput query="QueryName">
   #queryName.gla#
</cfoutput>

Use that if you have other things to put along side each row..

Or just do this..

 <cfoutput>#valueList(queryName.gla)#</cfoutput>

use this one if you just want the comma delimited list of values

Be sure to swap your query name in where I wrote "queryName"

0
 
mahpogAuthor Commented:
gdemaria,

I have to laugh at myself! The minute I read the ValueList, I knew it was the way I did the cfoutput.

I got my data and the result that I wanted.

Your solution is perfect.

Time for more coffee.......

thanks!

0
 
mahpogAuthor Commented:
I appreciated his detailed explanation and patience.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now