Active Query Builder support area

How to rename a table in the query (replace all occurrences) programmatically?

Last modified:


To rename a table in the query, you can iterate through the query datasources and change the name for those of them which refer to this table. The fine point is that you should call the QueryBuilder.Query.NotifySQLUpdatedRecursive() method to apply your changes to the query text. The code sample is provided below.
Dictionary renamedDataSources = new Dictionary();
renamedDataSources.Add("old", "new");

queryBuilder1.BeginUpdate();
try
{
	UnionSubQuery firstSelect = queryBuilder1.SQLQuery.QueryRoot.FirstSelect();
	List dataSources = new List();

	firstSelect.FromClause.GetDatasources(dataSources);
	for (int i = 0; i < dataSources.Count; i++)
	{
		DataSourceObject dataSourceObject = dataSources[i] as DataSourceObject;
		if (dataSourceObject != null)
		{
			SQLDatabaseObject databaseObject = dataSourceObject.DatabaseObject;
			string originalDataSourceName = databaseObject.QualifiedName;

			if (renamedDataSources.ContainsKey(originalDataSourceName))
			{
				databaseObject.RemoveLastName();
				databaseObject.AddName(renamedDataSources[originalDataSourceName]);
			}
		}
	}

	queryBuilder1.SQLQuery.NotifyUpdatedRecursive();
}
finally
{
	queryBuilder1.EndUpdate();
}
Dictionary renamedDataSources = new Dictionary();
renamedDataSources.Add("old","new");

queryBuilder1.BeginUpdate();
try
{
    UnionSubQuery firstSelect = queryBuilder1.Query.FirstSelect();
    System.Collections.ArrayList dataSourcesArrayList = new System.Collections.ArrayList();

    firstSelect.FromClause.GetDatasources(dataSourcesArrayList);
    foreach (DataSourceObject dataSourceObject in dataSourcesArrayList)
    {
        SQLDatabaseObject databaseObject = dataSourceObject.DatabaseObject;
        string originalDataSourceName = databaseObject.QualifiedNameWithoutQuotes;

        if (renamedDataSources.ContainsKey(originalDataSourceName))
        {
            databaseObject.RemoveLastName();
            databaseObject.AddName(renamedDataSources[originalDataSourceName]);
        }
    }
    queryBuilder1.Query.NotifySQLUpdatedRecursive();
}
finally
{
    queryBuilder1.EndUpdate();
}

Is this article helpful for you?