Solved

My CFQuery doesn't work, could you please help with the Where statement.

Posted on 2012-03-15
29
401 Views
Last Modified: 2012-08-14
I need your help.

Could you please view attached images and help build this CFQuery.

<CFQUERY NAME="DisplayUsertool" datasource="aies" username="#application.username#" password="#application.password#">
    SELECT CMTEE_PREF
    FROM cvitool
    WHERE

      <!--- (I want id_number ino aies.users = to id into dbo.cvitool - could you please advice on the colde or this query below) -->



                        </CFQUERY>
0
Comment
Question by:LelloLello
  • 17
  • 7
  • 5
29 Comments
 

Author Comment

by:LelloLello
ID: 37724765
My 3 SQL Tables.
tables.jpg
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37725003
I want id_number ino aies.users = to id into dbo.cvitool

Are those the right columns? "id_number" is an integer and cvitool.id is a varchar ..

What's the relationship between aies.users and dbo.cvitool ?
0
 

Author Comment

by:LelloLello
ID: 37725114
The ID_number into aies.users is always a six number ID.913057
The ID indo dbo.cvitool is the same a number where i'm having a nightly procedure copying all information from my IMIS to a SQL Database.  so is always a six number ID:
so that ID 913057always show into that field ID into dbo.cvitool.

FYI, Right now i'm able to retreive the first name and the last name and the ID_number from DisplayUser from USERS tables.

<CFQUERY NAME="DisplayUser" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT *
    FROM users
    WHERE users.user_id = #client.user_id#
    </CFQUERY>


So i've added a cfquerry to my code to retreive/show additional data into dbo.cvitool as CMTEE_PREF where ID Field into dbo.cvitool is the same as the id_number field into aies.users.

did u get it.  Please see tables images.

   <CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT *
    FROM cvitool
    WHERE
                   <!--- My where didn't work    users.user_id = #client.user_id#  --->
    </CFQUERY>

do you want me to copy all the code for you?
0
 

Author Comment

by:LelloLello
ID: 37725117
don't worry about datasources i've changed here on this page only.
0
 
LVL 4

Expert Comment

by:Rodrigo Munera
ID: 37725130
_agx_ is right, the columns on both tables have different data_types, you're going to have data_type mismatches across your application.

A word of advice, when you're coding foreign IDs between tables, give them the same name on both tables and the same datatype(length). It makes your life (and your fellow devs) a lot easier.

If I'm reading your question correctly you seem to be asking how to do a join query between to tables?

That's easy,
Select A.ID, A.name, B.report <!--- and the other columns you want to grab --->
FROM TableA as A
,TableB as B
WHERE A.ID=B.ID

If you're trying to get all columns on TableA and any matching columns on TableB, you have to do an OUTER join

Select <!--- Columns here --->
FROM TableA as A
LEFT OUTER JOIN TableB as B
ON A.ID=B.ID

You can do multiple OUTER table joins like so:

SELECT <!--- columns here --->
FROM TableA as A
LEFT OUTER JOIN TableB as B ON A.ID=B.ID
LEFT OUTER JOIN TableC as C ON B.bID=C.bID

The words LEFT and RIGHT tell SQL which side of the query you want to contain the other.

in the example above, you will get ALL records from table A, with all matching records from table B, and all matches from tableB will show a match from tableC and if there's no matching record then the column value for that record will have empty strings.

This is what's called a one-to-many table join. (I think)

EDIT: the first type of join, is called an INNER join, but SQL implicitly makes any joins inner if no other type is specified.

Think about 2 circles intersecting each other. The area where both circles intersect is the INNER join query results, when you do an OUTER join, you get the set of records in the intersect area and also all the other records in the side you specified. (LEFT or RIGHT)
0
 

Author Comment

by:LelloLello
ID: 37725161
ok sound goods. here my cfquery i would like to show the data in the field columns

So for your information my output is currenlty

Name (from users) it's working fine
Last (from users) it's working fine
ID_number (from user) it's displaying fine.
CMTEE_FREE (from cvitool) doesn't work for me, it's not displaying. I want to build a query for this one to display.

How I can fix that cfquery.

   <CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT *
    FROM cvitool
    WHERE users.user_id = #client.user_id#
    </CFQUERY>

<CFOUTPUT>
            <td ALIGN="LEFT" width="50%"><font size="3"><input type="text" name="CMTEE_PREF" value="#DisplayCVITOOL.CMTEE_PREF#"> </font></td>
            </CFOUTPUT>

thank you for your help.
0
 
LVL 4

Expert Comment

by:Rodrigo Munera
ID: 37725164
When you say "the code didn't work" do you mean, you're getting the wrong set of data that you were expecting? or are you actually getting a cold fusion / database error when you run the page?
It would help if you posted a screen capture of the cfdump of the data you are getting, or a capture screen of the cfcatch of the error you're getting

e.g.

<cftry>

<!--- Your code here --->
<cfquery name="myQuery">
</cfquery>
<cfdump var="#myQuery#">

<cfcatch type="any">
 <cfdump var="#cfcatch#">
</cfcatch>
</cftry>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37725193
<!--- My where didn't work    users.user_id = #client.user_id#  --->

Assuming you meant the "id" column, you also have to put the value in single quotes because the column type is  varchar.

         WHERE id = '#client.user_id#'

Even better, use cfqueryparam

         WHERE id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_varchar">

If the db's are on the same server you could also combine the 2 queries into one via a JOIN
0
 

Author Comment

by:LelloLello
ID: 37725207
okay i will try to use this one.

   <CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT *
    FROM cvitool
    WHERE id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_varchar">
    </CFQUERY>

<CFOUTPUT>
            <td ALIGN="LEFT" width="50%"><font size="3"><input type="text" name="CMTEE_PREF" value="#DisplayCVITOOL.CMTEE_PREF#"> </font></td>
            </CFOUTPUT>
0
 
LVL 4

Expert Comment

by:Rodrigo Munera
ID: 37725210
AH! as I suspected you're not joining the tables.

Your SQL Should be:

SELECT     users.Name
                ,users.last
                ,users.user_id
                ,cvitool.CMTEE_PREF
      FROM users,
                cvitool
    WHERE users.user_id=cvitool.user_id
        AND users.user_id = #client.user_id#



PS, I suggest never putting an open ## in your SQL, it makes your application to SQL injection which is very bad. I would suggest changing the last line with the following:

        AND users.user_id = <cfqueryparam cfsqltype="CF_SQL_INT" value="#client.user_id#">
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37725256
Yeah if the 2 db's are on the same server you can use a join like I suggested above. (Though the correct type is "CF_SQL_INTEGER" not "CF_SQL_INT")

1) I'd recommend using the newer ansi JOIN syntax, which will give you support for OUTER joins too.

2) You probably should CAST one of the columns since they're not the same type.  Either

FROM   users INNER JOIN cvitool ON cast(users.user_id as varchar) = cvitool.id
WHERE users.user_id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_integer">
... or

FROM   users INNER JOIN cvitool ON users.user_id  = cast(cvitool.id as INT)
WHERE users.user_id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_integer">

3) The fact that the columns are 2 different types is going to make the query a little slower. Because the db has to perform conversions every time. If possible, modify the second table to make the column an INT instead of a varchar
0
 
LVL 4

Expert Comment

by:Rodrigo Munera
ID: 37725295
Ah! thx _agx_ I wasn't sure about the cfsqltype, Yeah, I think they are, LelloLello has another thread with the whole text of the code and from there it seems like he's using the same datasource for both cfqueries.

Didn't know about the newer ANSI join syntax, do you know of a good resource where I can read more about it?
0
 

Author Comment

by:LelloLello
ID: 37725324
So my code was working before the first name and last name was displaying fine.... So i've just add that CFQUERY and this CFOUTPUT to display additional data from cvitool and it gave me an error. So I've attached the code for your review.

Here is my cfquerry added to my code.
---------------------------------------------------------
 <CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT users.firstName
                ,users.lastname
                ,users.id_number
                ,cvitool.CMTEE_PREF
    FROM users,
    cvitool
    WHERE users.user_id=cvitool.user_id
        AND users.user_id = <cfqueryparam cfsqltype="CF_SQL_INT" value="#client.user_id#">
    </CFQUERY>

-------------------------------------------
and here is my cfoutput added to my code
------------------------------------------
       
        <tr>
            <td align="right" bgcolor="78A8D2"><font color="white" size="2"><font face="arial" size="2"><b>CMTEE_PREF: </b></font></td>
            <CFOUTPUT>
            <td ALIGN="LEFT"><font size="3"><input type="text" name="CMTEE_PREF" value="#Trim(DisplayCVITool.CMTEE_PREF)#"> </font></td>
            </CFOUTPUT>
            </tr>
       
------------------------------------------
Here is the error message:
--------------------------------------------
CFSQLTYPE<P> 'CF_SQL_INT'  is not a valid CFSQLTYPE setting for CFQUERYPARAM. When specified, the CFSQLTYPE attribute must be set to one of the following: CF_SQL_BIGINT CF_SQL_BINARY CF_SQL_BIT CF_SQL_CHAR CF_SQL_CLOB CF_SQL_DATE CF_SQL_DECIMAL CF_SQL_DOUBLE CF_SQL_FLOAT CF_SQL_IDSTAMP CF_SQL_INTEGER CF_SQL_LONGVARBINARY CF_SQL_LONGVARCHAR CF_SQL_MONEY CF_SQL_MONEY4 CF_SQL_NUMERIC CF_SQL_REAL CF_SQL_REFCURSOR CF_SQL_SMALLINT CF_SQL_TIME CF_SQL_TIMESTAMP CF_SQL_TINYINT CF_SQL_VARBINARY CF_SQL_VARCHAR <p>The error occurred while processing an element with a general identifier of (CFQUERYPARAM), occupying document position (23:29) to (23:90) in the template file D:\USERS\ACTCA\HTML\MEMBERS\TOOLKIT\UPDATE_VOLUNTEER_INFO_E.CFM.</p><P><P>

Please advice. thank you.
update-volunteer-info.txt
0
 

Author Comment

by:LelloLello
ID: 37725341
FYI - Please note that i don't have a user_id into cvitool.  I have only a cvitool.ID which is equal to users.id_number.

WHERE users.user_id=cvitool.user_id
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:LelloLello
ID: 37725345
Please see the tables.jpg images.  it's very easy but i'm not sure where is the trick :( thank you for your help !!!
0
 

Author Comment

by:LelloLello
ID: 37725364
i have change the cfquerry to this one. What do you think... and it didn't work.

<CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT users.firstName
                ,users.lastname
                ,users.id_number
                ,cvitool.CMTEE_PREF
    FROM users,
    cvitool
    WHERE users.id_number=cvitool.id
        AND users.user_id = <cfqueryparam cfsqltype="CF_SQL_INT" value="#client.user_id#">
    </CFQUERY>
0
 
LVL 4

Accepted Solution

by:
Rodrigo Munera earned 250 total points
ID: 37725368
My apologies,
_agx_ pointed out my value for cfsql was wrong:

Here's the correct code:

<CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT users.firstName
                ,users.lastname
                ,users.id_number
                ,cvitool.CMTEE_PREF
    FROM users,
    cvitool
    WHERE users.user_id=cvitool.id
        AND users.user_id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#client.user_id#">
    </CFQUERY>
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 250 total points
ID: 37725380
EDIT: Oops... the posts keep overlapping

> <cfqueryparam cfsqltype="CF_SQL_INT" value="#client.user_id#">

See the code in my last response. It needs to be "CF_SQL_INTEGER" . You may also need to CAST one of the columns.  But try fixing the sql type first.

ie
FROM   users INNER JOIN cvitool ON users.user_id  = cast(cvitool.id as INT)
WHERE users.user_id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_integer">
0
 
LVL 4

Expert Comment

by:Rodrigo Munera
ID: 37725401
_agx_ is right, you're probably going to get type mismatch errors if you don't use CAST() like he suggested in his comment from 8:30
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37725416
Didn't know about the newer ANSI join syntax, do you know of a good resource where I can read more about it?

Well I shouldn't say "new", just newer ;-) IIRC it's part of the ANSI-92 specs. Though I didn't kick the old syntax habit till a few years ago. I don't know of any resources off hand. I mostly googled and read about it in the MS SQL BOL.
0
 

Author Comment

by:LelloLello
ID: 37725623
Here is my current code now. There is no error with this code below but NO DATE into the CFOUTPUT are displaying. Empty fields... but there is data.

 <CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT users.firstName
                ,users.lastname
                ,users.id_number
                ,cvitool.ID
                ,cvitool.AF_YR
                ,cvitool.CR_YR
                ,cvitool.Profile_updated
                ,cvitool.CMTEE_PREF
                ,cvitool.ISSUES
                ,cvitool.PRACTICE_AREA_SPEC    
                ,cvitool.YEARS_EXP                                          
                ,cvitool.PRACTICE_AREA_PAST
                ,cvitool.OTHER_ACT_VOL                                
                ,cvitool.OTHER_NONACT_VOL
                ,cvitool.SPEC_INT
                ,cvitool.SPEC_SKILLS                
                                               
    FROM users,
    cvitool
    WHERE users.user_id=cvitool.id
        AND users.user_id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#client.user_id#">
    </CFQUERY>

As per you request, i've changed my cfquery to

<CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT users.firstName
                ,users.lastname
                ,users.id_number
                ,cvitool.ID
                ,cvitool.AF_YR
                ,cvitool.CR_YR
                ,cvitool.Profile_updated
                ,cvitool.CMTEE_PREF
                ,cvitool.ISSUES
                ,cvitool.PRACTICE_AREA_SPEC    
                ,cvitool.YEARS_EXP                                          
                ,cvitool.PRACTICE_AREA_PAST
                ,cvitool.OTHER_ACT_VOL                                
                ,cvitool.OTHER_NONACT_VOL
                ,cvitool.SPEC_INT
                ,cvitool.SPEC_SKILLS                
                                               
    FROM users,
    cvitool
    FROM   users INNER JOIN cvitool ON users.user_id  = cast(cvitool.id as INT)
WHERE users.user_id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_integer">
    </CFQUERY>

----------------------------------------------------
and I got that the following error:
----------------------------------------------------
ODBC Error Code = 37000 (Syntax error or access violation)<P><P> [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'FROM'.<P><P>SQL = "SELECT users.firstName
                ,users.lastname
                ,users.id_number
                ,cvitool.ID
                ,cvitool.AF_YR
                ,cvitool.CR_YR
                ,cvitool.Profile_updated
                ,cvitool.CMTEE_PREF
                ,cvitool.ISSUES
                ,cvitool.PRACTICE_AREA_SPEC    
                ,cvitool.YEARS_EXP                                          
                ,cvitool.PRACTICE_AREA_PAST
                ,cvitool.OTHER_ACT_VOL                                
                ,cvitool.OTHER_NONACT_VOL
                ,cvitool.SPEC_INT
                ,cvitool.SPEC_SKILLS                
                                               
    FROM users,
    cvitool
    FROM   users INNER JOIN cvitool ON users.user_id  = cast(cvitool.id as INT)
WHERE users.user_id = ?"<P>
Query Parameter Value(s) - <P>Parameter #1 = 10777
<P>Data Source = "ACTUARIES"<P><p>The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (15:2) to (15:123) in the template file D:\USERS\ACTCA\HTML\MEMBERS\TOOLKIT\UPDATE_VOLUNTEER_INFO_E.CFM.</p><P><P>
0
 

Author Comment

by:LelloLello
ID: 37725625
DATE -> I mean DATA.
0
 
LVL 4

Expert Comment

by:Rodrigo Munera
ID: 37725638
You have two FROM statements. Make sure you only have 1
0
 

Author Comment

by:LelloLello
ID: 37725718
Ops, thank you. Okay there is no compilations errors. But my data doesn't show into my fields.

Please see image.  FYI, for id 885519 into dbo.cvitool there is data but it didn't display on the form. why ?

Only data into users where id_number 885519 was displayed.
NoData.jpg
0
 

Author Comment

by:LelloLello
ID: 37725826
i found out the error.   FROM   users INNER JOIN cvitool ON users.id_number  = cast(cvitool.id as INT)

so this is the correct cfquery.  Thank you for your help.

<CFQUERY NAME="DisplayCVITool" datasource="Actuaries" username="#application.username#" password="#application.password#">
    SELECT users.firstName
                ,users.lastname
                ,users.id_number
                ,cvitool.ID
                ,cvitool.AF_YR
                ,cvitool.CR_YR
                ,cvitool.Profile_updated
                ,cvitool.CMTEE_PREF
                ,cvitool.ISSUES
                ,cvitool.PRACTICE_AREA_SPEC    
                ,cvitool.YEARS_EXP                                          
                ,cvitool.PRACTICE_AREA_PAST
                ,cvitool.OTHER_ACT_VOL                                
                ,cvitool.OTHER_NONACT_VOL
                ,cvitool.SPEC_INT
                ,cvitool.SPEC_SKILLS                
 
    FROM   users INNER JOIN cvitool ON users.id_number  = cast(cvitool.id as INT)
WHERE users.user_id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_integer">
    </CFQUERY>
<!---     FROM   users INNER JOIN cvitool ON users.user_id  = cast(cvitool.id as INT)
WHERE users.user_id = <cfqueryparam value="#client.user_id#" cfsqltype="cf_sql_integer">
---->

so this is the correct cfquerry. Could you please stay tune, I have another request with this code that i'm trying to do.
0
 

Author Closing Comment

by:LelloLello
ID: 37725871
Thank you for your help !! i have another questions within coldfusion now.
0
 

Author Comment

by:LelloLello
ID: 37726102
0
 

Author Comment

by:LelloLello
ID: 37895002
0
 

Author Comment

by:LelloLello
ID: 37903139
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now