Unexpected token: WHERE System.QueryException

apexknowledgesoql

When i run the below query i am getting the following error , can some one help me to resolve this.

 if(searchvalue != null && articleType == null){
            String sQuery1 = 'Select Id, Title'+ 
                'from Knowledge__kav '+
                'Where Language=:lang AND Title Like:searchKey AND PublishStatus=\'online\''+
                'WITH DATA CATEGORY Knowledge_Base_Category__c BELOW Administrative__c';
            lstOfRecords = Database.query(sQuery1);
    }

Error : System.QueryException: unexpected token: Where

Best Answer

You're missing a space between the last field and from:

String sQuery1 = 'Select Id, Title '+ // Add a space here 
    'from Knowledge__kav ' + // ...

As a side note, if you're not using dynamic features, like variable fields, you should use an inline query:

lstOfRecords = [
  Select Id, Title from Knowledge__kav 
  Where Language = :lang AND 
    Title Like :searchKey AND 
    PublishStatus='online' 
  WITH DATA CATEGORY Knowledge_Base_Category__c BELOW Administrative__c];