incorrect syntax near the keyword 'where'
Main Topics
Browse All TopicsI am getting an error message when I try the following insert query, what am I doing wrong?
Here it is....
<cffile action = "read"
file = "c:\inetpub\wwwroot\coldfu
variable = "message">
<cftransaction>
<cfquery datasource="btadb">
delete from titles
where accountnbr = #session.accountnbr# and groupid = 0
</cfquery>
<cfloop index="line" list="#message#" delimiters="#chr(10)##chr(
<cfquery datasource="btadb">
INSERT INTO titles
(Accountnbr, groupid, titleid, titledescription)
VALUES
(#session.accountnbr#,
#listgetat(line, 2, ",")#,
#listgetat(line, 3, ",")#,
'#REReplace(listgetat(line
Where #listgetat(line, 1, ",")# = #session.accountnbr# and
#listgetat(line, 2, ",")# = 0
</cfquery>
</cfloop>
</cftransaction>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can only use the where clause in that context when your values are coming from a select statement. IE:
INSERT INTO titles (Accountnbr, groupid, titleid, titledescription)
SELECT Accountnbr, groupid, titleid, titledescription
FROM SomeOtherTable
WHERE SomeField = #SomeValue#
What you need to do is build a CFIF around the CFQUERY that does the expression check you are trying to accomplish in the WHERE clause:
<cfloop index="line" list="#message#" delimiters="#chr(10)##chr(
<cfif listgetat(line, 1, ",") EQ session.accountnbr AND listgetat(line, 2, ",") = 0>
<cfquery datasource="btadb">
INSERT INTO titles (Accountnbr, groupid, titleid, titledescription)
VALUES (#session.accountnbr#, #listgetat(line, 2, ",")#, #listgetat(line, 3, ",")#, '#REReplace(listgetat(line
</cfquery>
</cfif>
</cfloop>
looks like you had an extra )
<cfquery datasource="btadb">
INSERT INTO titles
(Accountnbr, groupid, titleid, titledescription)
VALUES
(#session.accountnbr#,
#listgetat(line, 2, ",")#,
#listgetat(line, 3, ",")#,
'#REReplace(listgetat(line
Where #listgetat(line, 1, ",")# = #session.accountnbr# and
#listgetat(line, 2, ",")# = 0
</cfquery>
there are two where clauses
if any of those vars are empty than you run into a problem
What is the value of #listgetat(line, 1, ",")# and #listgetat(line, 2, ",")# and also session.accountnbr too
This query looks suspicious:
INSERT INTO titles
(Accountnbr, groupid, titleid, titledescription)
VALUES
(#session.accountnbr#,
#listgetat(line, 2, ",")#,
#listgetat(line, 3, ",")#,
'#REReplace(listgetat(line
Where #listgetat(line, 1, ",")# = #session.accountnbr# and
#listgetat(line, 2, ",")# = 0
How are you inserting .. shouldn't this be an update?
update titles
set
col1 = val1,
...
where
....
try this isntead:
<cfif ListLen(line, ",") GT 2>
Update titles
set
groupid = #listgetat(line, 2, ",")#,
titleid = #listgetat(line, 3, ",")#,
titledescription = '#REReplace(listgetat(line
Where #listgetat(line, 1, ",")# = #session.accountnbr# and
#listgetat(line, 2, ",")# = 0
<cfelse>
line did not have enough elements
</cfif>
CJ
Am I wrong or shouldn't it be an update instead of an insert. esp with the where clause.
Unless you just remove the where clause and then just insert it.
then it would be:
<cfif ListLen(line, ",") GT 2>
INSERT INTO titles
(Accountnbr, groupid, titleid, titledescription)
VALUES
(#session.accountnbr#,
#listgetat(line, 2, ",")#,
#listgetat(line, 3, ",")#,
'#REReplace(listgetat(line
<cfelse>
line did not have enough elements
</cfif>
Hello there !
How about doing your query this way :
<cftransaction>
<cfquery datasource="btadb">
delete from titles
where accountnbr = #session.accountnbr# and groupid = 0
</cfquery>
<cfloop index="line" list="#message#" delimiters="#chr(10)##chr(
<cfif listgetat(line, 1, ",") eq #session.accountnbr# and listgetat(line, 2, ",") eq 0>
<cfquery datasource="btadb">
INSERT INTO titles
(Accountnbr, groupid, titleid, titledescription)
VALUES
(#session.accountnbr#,#lis
</cfquery>
</cfif>
</cfloop>
</cftransaction>
Goodluck!
eNTRANCE2002 :-)
Yes I agree with CJ & dc197 on that !
also i think one of ur lines may have extra single quotes ['] in it or something [i assume this, since u are reading from a file & inserting line by line]
use #PreserveSingleQuotes()# & also wrap ur values inside a CFQUERYPARAM - so it becomes much easier !
HTH
K'Rgds
Anand
Sorry I did not respond sooner, I figured out that I could not use the where clause with the insert query. I modified the data coming in, to only be the data that I want to insert, so I will not need the where clause anymore. I appreciate all of the response and I will give the points to shooksm. Thanks.
Business Accounts
Answer for Membership
by: pimpsuitPosted on 2003-10-07 at 07:11:21ID: 9505831
What is the error message?