動的SOQLに例文紹介します。
String tp = 'tptest';
String pt = 'pttest';
Date todayDate = Date.today();
String soql = 'SELECT Name ' +
' FROM Test__c ' +
' WHERE TestCo__c = ' + '\'' + this.co + '\''+
' and TestPt__c = ' + '\'' + pt + '\'' +
' and TestDt1__c <= : todayDate ' +
' and TestDt2__c >= : todayDate ';
if(!String.isBlank(tp)){
soql += ' and TestTp__c = ' + '\'' + tp + '\'' ;
}
soql += ' Order by Name ';
List<Test__c> results = Database.query(soql);