这是我的代码:
<cfdump var="#addEnt#" >
<!-- ADD -->
<cfquery name="add" datasource="testdatasource" dbtype="OLEDB">
UPDATE tblrequests
SET
lastname='#ucase(form.lastname)#',
firstname='#ucase(form.firstname)#',
middlei='#ucase(form.middlei)#',
title='#form.title#',
eod='#dateformat(form.eod,'m-d-yyyy')#',
dutystation='#form.dutystation#',
requestsnetwork=<cfif parameterexists(form.requestsnetwork)>1<cfelse>0</cfif>,
affiliation='#form.affiliation#',
commentssupvreq='#form.commentssupvreq#',
requestdelete=<cfif form.requestdelete IS NOT ''>'#dateformat(form.requestdelete,'m-d-yyyy')#',<cfelse>Null,</cfif>
commentssupvdelete='#form.commentssupvdelete#',
commentssupvedit='#form.commentssupvedit#',
dateemailrequested=<cfif form.dateemailrequested IS NOT ''>'#dateformat(form.dateemailrequested,'m-d-yyyy')#',<cfelse>Null,</cfif>
commentsit='#form.commentsit#',
bgcomplete=<cfif form.bgcomplete IS NOT ''>'#dateformat(form.bgcomplete,'m-d-yyyy')#',<cfelse>Null,</cfif>
dategroupscreated=<cfif form.dategroupscreated IS NOT ''>'#dateformat(form.dategroupscreated,'m-d-yyyy')#',<cfelse>Null,</cfif>
WHERE recnumber = #addEnt#
</cfquery>
当我提交表单时,我收到错误:
执行数据库查询时出错。 [Macromedia] [SQLServer JDBC 驱动程序] [SQLServer]关键字'WHERE'附近的语法不正确。
我的cfdump在sql中显示正确的addent
号码,但在sql语句中使用#addEnt#不起作用。我的应用程序##中的其他页面用于SQL查询,它们工作正常。
答案 0 :(得分:4)
set语句中的最后一行最后有一个逗号,这是SQL抱怨的地方
答案 1 :(得分:4)
(评论太长)
正如评论中所建议的那样,您可以对查询进行一些改进:其中一项最大的改进是添加cfqueryparam。它提供了几个优点,例如:
其他一些改进查询的提示
虽然它没有语法差别,但考虑在每行的开头放置逗号,而不是在结尾放置逗号。这样可以更容易地发现额外或缺少的逗号:
UPDATE SomeTable
SET ColumnA = 'xxxx'
, ColumnB = 'yyyy'
, ColumnC = 'zzzzz'
, ColumnD = 'xxxx'
, ColumnE = 'yyyy'
WHERE ....
看起来您的查询正在填充多个日期时间列。使用datetime列时,最好使用日期对象,而不是字符串。日期字符串不明确,可能会有不同的解释,具体取决于数据库或设置。要仅插入日期,请使用<cfqueryparam cfsqltype="cf_sql_date" ...>
,日期和时间使用<cfqueryparam cfsqltype="cf_sql_timestamp" ...>
。显然,始终首先验证日期字符串。
考虑使用cfqueryparam的null
属性。有条件地插入空值时非常方便。 (见下面的例子)
除此之外,ParameterExists不久前已被弃用,并替换为IsDefined,或者最好是StructKeyExists。在这种情况下,CFIF的另一种替代方法是使用cfparam声明默认值,因此相关的表单字段始终存在。
总而言之,您的最终查询可能看起来像这样。我猜测了列数据类型,因此根据需要进行调整。
UPDATE tblrequests
SET lastname = <cfqueryparam value="#ucase(form.lastname)#" cfsqltype="cf_sql_varchar">
, firstname = <cfqueryparam value="#ucase(form.firstname)#" cfsqltype="cf_sql_varchar">
, middlei = <cfqueryparam value="#ucase(form.middlei)#" cfsqltype="cf_sql_varchar">
, title = <cfqueryparam value="#form.title#" cfsqltype="cf_sql_varchar">
, eod = <cfqueryparam value="#form.eod#" cfsqltype="cf_sql_date">
, dutystation = <cfqueryparam value="#form.dutyStation#" cfsqltype="cf_sql_varchar">
, requestsnetwork = <cfqueryparam value="#form.requestsNetwork#" cfsqltype="cf_sql_bit">
, affiliation = <cfqueryparam value="#form.affiliation#" cfsqltype="cf_sql_varchar">
, commentssupvreq = <cfqueryparam value="#form.commentsSupvReq#" cfsqltype="cf_sql_varchar">
, requestdelete = <cfqueryparam value="#form.requestDelete#" cfsqltype="cf_sql_date" null="#not isDate(form.requestDelete)#">
, commentssupvdelete = <cfqueryparam value="#form.commentssupvdelete#" cfsqltype="cf_sql_varchar">
, commentssupvedit = <cfqueryparam value="#form.commentssupvedit#" cfsqltype="cf_sql_varchar">
, dateemailrequested = <cfqueryparam value="#form.dateEmailRequested#" cfsqltype="cf_sql_date" null="#not isDate(form.dateEmailRequested)#">
, commentsit = <cfqueryparam value="#form.commentsit#" cfsqltype="cf_sql_varchar">
, bgcomplete = <cfqueryparam value="#form.bgComplete#" cfsqltype="cf_sql_date" null="#not isDate(form.bgComplete)#">
, dategroupscreated = <cfqueryparam value="#form.dateGroupsCreated#" cfsqltype="cf_sql_date" null="#not isDate(form.dateGroupsCreated)#">
WHERE recnumber = <cfqueryparam value="#addEnt#" cfsqltype="cf_sql_integer">