+ Start a Discussion
asadimasadim 

SOQL: Select everything?

Hi,

 

Is there a way to select everything in SOQL like "SELECT *" in SQL? I have looked at the SOQL manual about SELECT statements but it didn't tell if I can do this or not.

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
ArtabusArtabus

No, you can't do this in SOQL, but you can generate dynamically your SOQL query with the list of all fields (Id, Name, etc...).

To identify all fields, you can use the API call describeSObjects()

All Answers

ArtabusArtabus

No, you can't do this in SOQL, but you can generate dynamically your SOQL query with the list of all fields (Id, Name, etc...).

To identify all fields, you can use the API call describeSObjects()

This was selected as the best answer
asadimasadim
Thanks.
John CasimiroJohn Casimiro

Hi,

 

I did a write up on how this is accomplished for people who happen to stumble upon this topic.  It can be found here,

 

http://johncasimiro.posterous.com/mimic-a-sql-select-statement-in-soql-using-ap

jdance1.3948195933250098E12jdance1.3948195933250098E12
Posterous died. Any other examples of how to do this? 
Paul TetreauPaul Tetreau
there's probably a cleaner way to do it, but this is the hack i used:

SELECT * FROM table_name

becomes

String table = 'table_name';
String query_string = 'SELECT ';
List<String> table_list = new List<String>();
table_list.add(table);

Set<String> table_rows = schema.describeSObjects(table_list)[0].fields.getMap().keyset();
for(String row : table_rows){
    query_string += row + ',';
}
query_string = query_string.removeEnd(','); //remove trailing comma
query_string += ' FROM ' + table;

Database.query(query_string);
Aaron LevensailorAaron Levensailor
Thanks, Paul.  I suggest the following adaptation.
public static List<SObject> selectStar(String sobjectName) {
    Set<String> fieldNames = schema.describeSObjects(new String[] {sobjectName})[0].fields.getMap().keyset();
    List<String> iterableFields = new List<String>(fieldNames);

    return Database.query(String.format('SELECT {0} FROM {1}', new String[] {String.join(iterableFields, ','), sobjectName}));
}