function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rashrash 

Dynamic SOQL

I am writing a SOQL query where both field name and field value should be dynamic.  Can anyone suggest how this can be done? Please provide some example.
okaylah52okaylah52


Message Edited by okaylah52 on 07-22-2008 02:11 AM
rashrash
Thanx...  but what i need is , for eg:
 
String FieldName= 'Name';
String var= 'abc';
 
Account a = [ select  FieldName  from  Account where Field = : var];
 
Is this possible or is there any other solution for this?
 
okaylah52okaylah52
Something like below?

String fieldName = 'Name';
String target = 'abc';
String sql = 'select ' + fieldName + ' from Account where field = \'' + target + '\'';
List<Account> result = Database.query(sql);

(I haven't tried this but, from documentation, it should work. Give it a try. Hope this helps)
rashrash

Hey thanks.. this works!!!!