+ Start a Discussion
Team WorksTeam Works 

Database.Query -- How to concaenate this SOQL as string

I want to concatenate two strings to for the query to be executed using Database.Query. Here is what i am doing
string sql1 = 'Select Custom_Name__c from Opportunity  where AccountId IN';
string2 sql2= '(Select Id from Account where Europe__c =True)';
Opp=Database.query(sql1+sql2);
sysem.debug(Opp);
Doesn't work.Any Ideas?
Best Answer chosen by Team Works
Vamsi KrishnaVamsi Krishna
Hi

not sure what exactly your requirments are.. and why you need dynamic soql for this.. but you can try the below code..
string sql1 = 'Select Custom_Name__c from Opportunity  where AccountId IN ';
string sql2= '(Select Id from Account where Europe__c =True)';
List<sobject> Opps = Database.query(sql1+sql2);
sysem.debug(Opps.size());

All Answers

Team WorksTeam Works
Also If i wanted use this query-- Select Name from Account where Id IN : ListofIds; AS

private final String SOQL_START_FUNDS = 'SELECT Id,Name FROM Account';
List<Account> result = Database.Query(SOQL_START_FUNDS + ' WHERE Id IN :ListofIds\''); //Does't work
Need Help!!
Vamsi KrishnaVamsi Krishna
Hi

not sure what exactly your requirments are.. and why you need dynamic soql for this.. but you can try the below code..
string sql1 = 'Select Custom_Name__c from Opportunity  where AccountId IN ';
string sql2= '(Select Id from Account where Europe__c =True)';
List<sobject> Opps = Database.query(sql1+sql2);
sysem.debug(Opps.size());

This was selected as the best answer
Team WorksTeam Works
Thanks Vamsi.Any ideas about useing the Bind Variable with List in Database.Query?
Team WorksTeam Works
Thanks Vamsi ...I was able to do it..
Team WorksTeam Works
Hello Guys

Again Stuck with the concatenation :

private final String SOQL_RECENT_REC = 'SELECT LastViewedDate,Type,UserRoleId FROM RecentlyViewed';
private final String CONDITION_USER_SORT = ' Order By LastViewedDate DESC LIMIT 20';
List<RecentlyViewed> recentViewed= new List<RecentlyViewed>();
recentViewed = Database.query(SOQL_RECENT_REC +' WHERE Type = \'Account \' ' + CONDITION_USER_SORT);

Should run a query like the following :
recentViewed = [SELECT LastViewedDate,Type,UserRoleId FROM RecentlyViewed Where Type='Account' Order By LastViewedDate DESC LIMIT 20];

Many Thanks !!