I want to run a search in my model for an element by its name or its acronym
I have created a custom sql search, here is the sql:
select ea_guid AS CLASSGUID, object_type AS CLASSTYPE from t_object
where t_object.Name = '#WC#(<Search Term>)#WC#'
or t_object.Name = '<Search Term>'
or t_object.Alias = '<Search Term>'
when I try to run it with Repository.GetElementsByQuery in my c# addin, it gives me an error:
To run a custom search you must specify ea_guid AS CLASSGUID in your select statement
However, ea_guid and object_type are clearly there
Is this not possible to do with an add-in? Any reason why?
The model is hosted on a SQLServer
I am running EA 13 build 1309
EDIT: I am running Repository.SQLQuery as a workaround for now
You haven't posted the code that actually gives you the error, making it harder to see what is going wrong.
Anyway, there are two ways you can use GetElementsByQuery()
1) by passing the name of your query as parameter like such:
EA.Repository.GetElementsByQuery("NameOfMySearch",1);
in this case you can't provide the search parameter, so that is only useful if you have a search that doesn't depend on the <Search Term>
2) by passing the actual SQL code tot the method. In you case that would be something like:
string mySearchTerm = "somethingImLookingFor";
string SQLSearchString = @"select ea_guid AS CLASSGUID, object_type AS CLASSTYPE from t_object
where t_object.Name = '%" + mySearchTerm +@ "%'
or t_object.Alias = '" + mySearchTerm + "'";
var foundElements = Repository.GetElementsByQuery(SQLSearchString ,2);
The second parameter in this case needs to be "2". This second method is the way I always use this operation.
Also take care to use the correct SQL syntax depending on the backend you are using. This will work for SQL server, but not for .eap files.