如何为每次插入和每次循环迭代增加计数 ID?
How to increment count id for each insert and every loop iteration?
我有一个 for 循环,它应该为每个查询和每个循环迭代递增 count_id。这是我的代码示例:
qryCode = queryExecute("SELECT max(display_order) AS maxdisplay FROM type_ref",{},{datasource: application.datasource}); // Result set of the query is: 58
qryItems = queryExecute("SELECT DISTINCT type_id, label, type_shortcode FROM types gtr WHERE item_id = :item_id",{item_id: {cfsqltype: "cf_sql_numeric",value: arguments.item_id}},{datasource: application.datasource});
// Result set of qryItems:
TYPE_ID LABEL TYPE_SHORTCODE
1 2012-1 HOA
2 2012-1 HOC
5 2012-1 HOR
local.display_count = qryCode.maxdisplay;
for ( row in qryItems ) {
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+1});
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+2});
display_count++;
}
上面的代码将增加前两个正确的值(59 和 60),但对于第二次迭代,它将从 60 而不是 61 开始。代码应该生成 count_id 的 int 他的顺序:59,60,61,62,63,64
。 qryItems
中有3条记录。 qryCode
的最大值为 58
。第一次迭代中的第一个查询应该从 58 + 1 = 59
开始。下一个应该是58 + 2 = 60
。在第二次迭代中,第一个 count_id 应该是 61 等等。我不确定为什么我上面的代码从 60 而不是 61 开始第二次迭代。我确实有这一行应该在每次迭代结束时增加 count_id:display_count++;
.
这是因为您每次迭代执行 2 次插入,因此您应该将 display_count
递增 2 而不是 1。因此您的 for 循环应该看起来像这样。
for ( row in qryItems ) {
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+1});
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+2});
display_count +=2;
}
怎么样
for ( row in qryItems ) {
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?),(?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: ++display_count});
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: ++display_count});
}
另见:Inserting multiple rows in a single SQL query?
我有一个 for 循环,它应该为每个查询和每个循环迭代递增 count_id。这是我的代码示例:
qryCode = queryExecute("SELECT max(display_order) AS maxdisplay FROM type_ref",{},{datasource: application.datasource}); // Result set of the query is: 58
qryItems = queryExecute("SELECT DISTINCT type_id, label, type_shortcode FROM types gtr WHERE item_id = :item_id",{item_id: {cfsqltype: "cf_sql_numeric",value: arguments.item_id}},{datasource: application.datasource});
// Result set of qryItems:
TYPE_ID LABEL TYPE_SHORTCODE
1 2012-1 HOA
2 2012-1 HOC
5 2012-1 HOR
local.display_count = qryCode.maxdisplay;
for ( row in qryItems ) {
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+1});
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+2});
display_count++;
}
上面的代码将增加前两个正确的值(59 和 60),但对于第二次迭代,它将从 60 而不是 61 开始。代码应该生成 count_id 的 int 他的顺序:59,60,61,62,63,64
。 qryItems
中有3条记录。 qryCode
的最大值为 58
。第一次迭代中的第一个查询应该从 58 + 1 = 59
开始。下一个应该是58 + 2 = 60
。在第二次迭代中,第一个 count_id 应该是 61 等等。我不确定为什么我上面的代码从 60 而不是 61 开始第二次迭代。我确实有这一行应该在每次迭代结束时增加 count_id:display_count++;
.
这是因为您每次迭代执行 2 次插入,因此您应该将 display_count
递增 2 而不是 1。因此您的 for 循环应该看起来像这样。
for ( row in qryItems ) {
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+1});
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: display_count+2});
display_count +=2;
}
怎么样
for ( row in qryItems ) {
local.sqlInsert &= " INSERT INTO type_ref (display_order) VALUES (?),(?) ";
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: ++display_count});
qryParams.append({cfsqltype="CF_SQL_NUMERIC", value: ++display_count});
}
另见:Inserting multiple rows in a single SQL query?