我在while select查询中将记录插入临时表。如果我之前添加了记录,我需要控制它,是否可能?
答案 0 :(得分:2)
如果您有唯一索引you can catch the problem:
SysInfoLogLevel prevLevel = infolog.infologLevel()
try
{
infolog.setInfoLogLevel(SysInfoLogLevel::None);
whatever.insert();
infolog.setInfoLogLevel(prevLevel);
}
catch (Exception::DuplicateKeyException)
{
infolog.setInfoLogLevel(prevLevel);
}
答案 1 :(得分:1)
在调用插入过程之前,只需检查记录。
//Build query above
while (qr.next())
{
SourceTable = qr.get(tablenum(SourceTable));
select DestTable
where DestTable.Field1 == SourceTable.Field1; //Replace with criteria that determines if records are the same
if (!DestTable)
{
DestTable.Field1 = SourceTable.Field1;
DestTable.Field2 = SourceTable.Field2;
DestTable.Field3 = SourceTable.Field3;
DestTable.Insert();
}
}
//Once processing is finished
if (element.prompt())
{
while select DestTable
{
element.send(DestTable);
}
}
else
{
return false;
}
基本上,尝试选择与插入的匹配的记录。如果没有返回记录,则插入它。否则它只会进入下一条记录。
答案 2 :(得分:1)
在相同类型的第二个缓冲区上使用setTmpData方法
示例:
SomeTable someTable;
TmpTable tmpTable;
TmpTable tmpTableCheck;
// Use tmpTable as primary data set, and tmpTableCheck to check for duplicates
while select someTable
{
tmpTable.initFromSomeTable(someTable);
// Check if key is already used.
tmpTableCheck.setTmpData(tmpTable);
select firstOnly tmpTableCheck
where tmpTableCheck.Key == tmpTable.Key;
// Only insert record in tmpTable if the key has not been used before.
if (! tmpTableCheck)
{
tmpTable.insert();
}
}