将 ColdFusion 应用程序生成的标识值插入 iSeries 表的最佳方法是什么?

发布于 2024-11-28 16:26:39 字数 1696 浏览 6 评论 0原文

我正在使用没有自动递增标识列的数据库(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 语句中调用该函数有哪些优点/缺点:

  1. 在 INSERT 语句内生成 ID

    
        插入我的表            
            (
                ID,
                名称文本
            )
        SELECT #getNewTableId('MyTable','ID')#,
                
    
    
  2. 在 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:

  1. 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>
    
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2024-12-05 16:26:39

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.

似梦非梦 2024-12-05 16:26:39

无论哪种方式,你似乎都有重复的可能性。

假设两个用户同时执行相同的操作,并且查询获取最大 id 需要 500 毫秒(很长一段时间,但用于示例目的),这将不可避免地创建重复的 id。两种解决方案:

  1. 将 getID 查询和插入查询都封装在一个事务中。
  2. 使用某种聚集主键(例如 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:

  1. Encapsulate both the getID query and the insert query in a transaction.
  2. Redo your table using some sort of clustered primary key, such as id and date.

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?"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文