问题
I want to loop over a few tables where the name (aotName) begins with 'HbcCka', finally I want to delete all data from the tables.
Now I need a logic to empty the tables. How can I achieve this?
UPDATED:
static void tstDeleteForecastingData(Args _args)
{
Dictionary dictionary = new Dictionary();
int i;
SysDictTable dictTable;
;
for (i=1 ; i<=dictionary.tableCnt() ; i++)
{
if (strScan(tableid2name(dictionary.tableCnt2Id(i)), "HbcCka", 1, strLen(dictionary.tableName(i))))
{
info(strfmt('%1;%2', dictionary.tableCnt2Id(i), tableid2name( dictionary.tableCnt2Id(i) )));
}
}
}
回答1:
Truncate the tables in all companies?
Use System Administration\Periodic\Databases\SQL Administration, mark the tables then choose Table actions\Truncate.
Or make a class with a server main method:
ClassDeclaration tstDeleteForecastingData
{
}
static server void main(Args _args)
{
Dictionary dictionary = new Dictionary();
int i;
for (i=1 ; i<=dictionary.tableCnt() ; i++)
{
if (strScan(tableid2name(dictionary.tableCnt2Id(i)), "HbcCka", 1, 99)))
{
info(strfmt('%1;%2', dictionary.tableCnt2Id(i), tableid2name( dictionary.tableCnt2Id(i) )));
new SqlDataDictionaryPermission(methodstr(SqlDataDictionary, tableTruncate)).assert();
new SqlDataDictionary().tableTruncate(dictionary.tableCnt2Id(i), false);
CodeAccessPermission::revertAssert();
}
}
}
If only in current company:
Common table = new DictTable(<tableId>).makeRecord();
table.skipDeleteMethod(true);
table.skipDeleteAction(true);
delete_from table;
回答2:
To speed things up, you could use the stored procedure sp_msforeachtable to loop tables in the database (with a check on the table names) and just write your delete statement to delete records where the dataAreaId is the one you want.
Something like this can be use to delete the tables starting with HbcCka in the CEU company:
exec sp_msforeachtable '
if "?" like "HbcCka%"
delete from ? where DataAreaId = "CEU"
'
来源:https://stackoverflow.com/questions/13821709/how-can-i-loop-over-tables-which-contain-specific-characters-in-their-aot-name-a