You need to sign in to do that
Don't have an account?
vickySFDC
What is diff between Static soql and Dynamic soql queries?give some examples
Hi All,
Diff between Dynamic SOQL and Static SOQL queries?Is there any performance will increase while using stacic or dynamic queries?
Thanks,
Vicky
Dynamic SOQL refers to the creation of a SOQL string at runtime with Apex code. Dynamic SOQL enables you to create more flexible applications. For example, you can create a search based on input from an end user, or update records with varying field names.....this is the major difference between soql and dynamic soql
dynamic query looks like this...
List<sObject> L = Database.query(string);
For more reference go through this link
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.
If it helps you give kudos and mark it as solutions
Thanks
STATIC VS DYNAMIC?
Static SOQL is one which you write in square brackets. It is good to use when you didn't have any dynamic changes in the soql query.
For e.g when the fields names or where conditions is needed to be defined dynamically we didn't use statis soql. Then we need to use dynamic soql.
Dynamic SOQL is dynamic query creation
String soql = 'SELECT id, name FROM Account';
List<Account> accList = Database.query(soql);
One DISADVANTAGE with DYNAMIC SOQL is it causes SOQL injection in where condition which fetching on the basis of some text. To avoid which we need to use String.escapeSingleQuotes. There is no possiblily of these in STATIS SOQL.
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
Well to check the performance, you can always write a testClass which inserts records in bulk and queries the same using a Dynamic Query and a Static Query.
Check the query execution time and I guess you will have the answer.(Make sure you do the test atleast 5-6 times since other operations can affect the SOQL time)
One DISADVANTAGE with DYNAMIC SOQL is it causes SOQL injection in where condition which fetching on the basis of some text. To avoid which we need to use String.escapeSingleQuotes. There is no possiblily of these in STATIS SOQL
Just to clarify, Dynamic SOQL doesn't cause SOQL injection, it is vulnerable to injection. This means that users can bypass the intended query with some expanded query. Normally, this isn't a problem, though, since properly configured sharing and profile settings will prevent the user from seeing anything they couldn't see anyways. The probably biggest single problem with SOQL injection is when the page runs without security ("without sharing") or is using a shared login.
Yes I mean
To prevent a SOQL injection attack, we avoid using dynamic SOQL queries. Instead, use we static queries.
The term cause is not correct.
Thanks
1. Static SOQL :
Static SOQL statement is written in [ ] array brackets.
These statements are similar to LINQ
For Example :
=================================================
string searchfor = 'SalesforceKid' ;
Contact[] Contacts = [SELECT xyz__c, Firstname, Lastname FROM Contact WHERE Lastname = : searchfor];
=================================================
2. Dynamic SOQL :
- It is used to refer to the collection of a SOQL string at runtime with the apex code.
- Dynamic SOQL enables you to create a more flexible application.
- To create a Dynamic SOQL query at runtime use Database.Query() method, in one of the following ways.
- It returns a single sObject when the query returns a single record.
- For Example : sObject s = Database.query(string_limit_1)
- It returns a list of sObject when a query returns more than a single record.
For Example :
=================================================
string myTestString = 'TestName' ;
List<sObject> sl = Database.query(SELECT Id,Name FROM myCustomObject__c WHERE Name=: myTestString);
=================================================
To know more about SOQL Please visit the link below to learn from very basics :
- https://www.salesforcekid.com/2019/05/salesforce-soql-basics-part-14.html
Thanks & Regards,
Ajinkya Dhas ☁️⚡️
http://www.salesforcekid.com
Please mark this as the best answer if this information is useful for you.
Happy Learning ☁️⚡️
Kindly find solution.
Static Query:
Static SOQL is written in ([]) array brackets. Its good to use when you did not have dynamic chnages is SOQL.
Dynamic Query:
Dynamic SOQL refers to the creation of a SOQL string at run time with Apex code. Dynamic SOQL enables you to create more flexible applications. For example, you can create a search based on input from an end user or update records with varying field names.
String myTestString = 'TestName'; If you find your Solution than mark as this as a best answer.
Thanks and Regards
Suraj Tripathi.
1)Static SOQL
2)Dynamic SOQL
Static SOQL=>Static SOQL statement is written in [ ] array brackets.
For example:
List<Contact> con=[Select Id, LastName from Contact]
Dynamic SOQL => refers to the creation of a SOQL string at runtime with Apex code. Dynamic SOQL enables you to create more flexible applications.To create dynamic query at runtime, we use Database.query() method.
For example:
String test = 'SELECT id, name FROM Account';
List<Account> accList = Database.query(test);
database.query()=>Method
database.query allows you to make a dynamic SOQL query at runtime. You can build up a string and then use that as a query string at run time in the database.