将 ColdFusion 应用程序生成的标识值插入 iSeries 表的最佳方法是什么?
我正在使用没有自动递增标识列的数据库(iSeries)。因此,我必须在应用程序中即时生成 ID。
我有一个用户定义的函数可以为我完成此任务,因为我需要为多个表生成这些 ID:
<cffunction name="getNewTableId" returntype="numeric" output="false">
<cfargument name="TableName" type="string" required="true" />
<cfargument name="ColumnName" type="string" required="true" />
<cfargument name="SeedNum" type="numeric" required="false" default="1" />
<cfquery name="qMaxId" datasource="#REQUEST.DSN#">
SELECT MAX(#ARGUMENTS.ColumnName#) AS Id
FROM #ARGUMENTS.TableName#
</cfquery>
<cfscript>
if (qMaxId.RecordCount && IsValid("integer", qMaxId.Id))
return qMaxId.Id + 1;
return ARGUMENTS.SeedNum;
</cfscript>
</cffunction>
(我知道这不是很安全,但这只是为了让它在开发中工作) )
我的问题是,与在本地保存 ID 值相比,从 INSERT 语句中调用该函数有哪些优点/缺点:
在 INSERT 语句内生成 ID
插入我的表 ( ID, 名称文本 ) SELECT #getNewTableId('MyTable','ID')#, 在 INSERT 语句之前生成 ID
> 插入我的表 ( ID, 名称文本 ) 选择#newId#,
我倾向于第一个选项,因为 ColdFusion 将准备语句并立即执行它,而不是在本地存储值,然后让 ColdFusion 准备 SQL 语句。
有什么区别吗?
I am working with a Database (iSeries) that does not have auto-incrementing Identity columns. Thus, I have to generate the ID on-the-fly in the application.
I have a user-defined function that accomplishes this for me, since I need to generate these IDs for several tables:
<cffunction name="getNewTableId" returntype="numeric" output="false">
<cfargument name="TableName" type="string" required="true" />
<cfargument name="ColumnName" type="string" required="true" />
<cfargument name="SeedNum" type="numeric" required="false" default="1" />
<cfquery name="qMaxId" datasource="#REQUEST.DSN#">
SELECT MAX(#ARGUMENTS.ColumnName#) AS Id
FROM #ARGUMENTS.TableName#
</cfquery>
<cfscript>
if (qMaxId.RecordCount && IsValid("integer", qMaxId.Id))
return qMaxId.Id + 1;
return ARGUMENTS.SeedNum;
</cfscript>
</cffunction>
(I know this is not very secure, but this is bare-bones just to get it working int he development environment for now.)
My question is, what are the benefits/drawbacks of calling the function from within the INSERT statement, as opposed to saving the ID value locally:
Generate ID inside INSERT statment
<cfquery datasource="#REQUEST.DSN#"> INSERT INTO MyTable ( ID, NameTxt ) SELECT #getNewTableId('MyTable','ID')#, <cfqueryparam value="#FORM.MyName#" cfsqltype="CF_SQL_VARCHAR" maxlength="20" /> </cfquery>
Generate ID prior to INSERT statment
<cfset newId = getNewTableId('MyTable','ID') /> <cfquery datasource="#REQUEST.DSN#"> INSERT INTO MyTable ( ID, NameTxt ) SELECT #newId#, <cfqueryparam value="#FORM.MyName#" cfsqltype="CF_SQL_VARCHAR" maxlength="20" /> </cfquery>
I tend to lean towards the first option, since ColdFusion willi prepare the statement and execute it immediately, as opposed to store the value locally, and then having ColdFusion prepare the SQL statement.
Is there any difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AS/400 支持在列定义上使用
GENERATED ... AS IDENTITY
自动递增标识列。请参阅 SQL 参考:CREATE TABLE< /a> 声明以获取更多详细信息。
The AS/400 supports auto-incrementing identity columns using
GENERATED ... AS IDENTITY
on the column definition.See the SQL Reference: CREATE TABLE statement for more details.
无论哪种方式,你似乎都有重复的可能性。
假设两个用户同时执行相同的操作,并且查询获取最大 id 需要 500 毫秒(很长一段时间,但用于示例目的),这将不可避免地创建重复的 id。两种解决方案:
至于是否最好将 id 存储为变量,最好这样说:“我是否会在此请求中稍后的任何时候使用该 id?”
Either way you seem to have the possibility of duplication.
Let us say that two user perform the same action at the same time, and the query to get the max id takes 500ms (long time, but for example purposes), this will inevitably create a duplicate id. Two solutions:
As far as wether it is better to store the id as a variable or not is a question better said as, "Am I going to use this id at any point later in this request?"