My CFQuery doesn't work, could you please help with the Where statement.
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) -->
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 ?
LelloLello
ASKER
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>
don't worry about datasources i've changed here on this page only.
Rodrigo Munera
_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)
LelloLello
ASKER
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>
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#">
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#">
_agx_
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
Rodrigo Munera
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?
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
------------------------------------------
------------------------------------------
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>
_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
_agx_
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.
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>
LelloLello
ASKER
DATE -> I mean DATA.
Rodrigo Munera
You have two FROM statements. Make sure you only have 1
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.
LelloLello
ASKER
Thank you for your help !! i have another questions within coldfusion now.