【问题标题】:How can I loop over Tables which contain specific characters in their AOT name and delete the data?如何遍历其 AOT 名称中包含特定字符的表并删除数据?
【发布时间】:2012-12-11 13:42:03
【问题描述】:

我想遍历几个名称(aotName)以'HbcCka'开头的表,最后我想从表中删除所有数据。

现在我需要一个逻辑来清空表格。我怎样才能做到这一点?

更新:

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) )));
        }
    }

}

【问题讨论】:

    标签: x++ axapta


    【解决方案1】:

    截断所有公司的表格?

    使用 System Administration\Periodic\Databases\SQL Administration,标记表,然后选择 Table actions\Truncate。

    或者用服务器主方法创建一个类:

    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();
            }
        }
    
    }
    

    如果在当前公司:

    Common table = new DictTable(<tableId>).makeRecord();
    table.skipDeleteMethod(true);
    table.skipDeleteAction(true);
    delete_from table;
    

    【讨论】:

    • 只针对当前公司,所以我实现了第二个选项。非常感谢。
    【解决方案2】:

    为了加快速度,您可以使用存储过程 sp_msforeachtable 循环数据库中的表(检查表名),然后编写您的删除语句来删除 dataAreaId 是您想要的记录。

    这样的东西可以用来删除CEU公司中以HbcCka开头的表:

    exec sp_msforeachtable '
            if "?" like "HbcCka%"
            delete from ? where DataAreaId = "CEU"
            '
    

    【讨论】:

    • 速度会一样,因为 X++ delete_from table 会转换成相同的 SQL。
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 2015-05-29
    • 1970-01-01
    • 2020-05-03
    • 2020-06-18
    • 2013-10-04
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多