0

Why SQL is better than procedures?

21st Mar 2017, 3:42 PM
rohit paul
rohit paul - avatar
5 Answers
+ 1
In my experience Use Stored Procedures: For any complex data work. If you're going to be doing something truly requiring a cursor or temp tables it's usually fastest to do it within SQL Server. When you need to lock down access to the data. If you don't give table access to users (or role or whatever) you can be sure that the only way to interact with the data is through the SP's you create. Use ad-hoc queries: For CRUD when you don't need to restrict data access (or are doing so in another manner). For simple searches. Creating SP's for a bunch of search criteria is a pain and difficult to maintain. If you can generate a reasonably fast search query use that. In most of my applications I've used both SP's and ad-hoc sql, though I find I'm using SP's less and less as they end up being code just like C#, only harder to version control, test, and maintain. I would recommend using ad-hoc sql unless you can find a specific reason not to.
23rd Mar 2017, 6:46 AM
Akwin Lopez
Akwin Lopez - avatar
+ 1
SQL Server basically goes through these steps to execute any query (stored procedure call or ad-hoc SQL statement): 1) syntactically check the query 2) if it's okay - it checks the plan cache to see if it already has an execution plan for that query 3) if there is an execution plan - that plan is (re-)used and the query executed 4) if there is no plan yet, an execution plan is determined 5) that plan is stored into the plan cache for later reuse 6) the query is executed The point is: ad-hoc SQL and stored procedures are treatly no differently. If an ad-hoc SQL query is properly using parameters - as it should anyway, to prevent SQL injection attacks - its performance characteristics are no different and most definitely no worse than executing a stored procedure. Stored procedure have other benefits (no need to grant users direct table access, for instance), but in terms of performance, using properly parametrized ad-hoc SQL queries is just as efficient as using stored procedures. Update: using stored procedures over non-parametrized queries is better for two main reasons: since each non-parametrized query is a new, different query to SQL Server, it has to go through all the steps of determining the execution plan, for each query (thus wasting time - and also wasting plan cache space, since storing the execution plan into plan cache doesn't really help in the end, since that particular query will probably not be executed again) non-parametrized queries are at risk of SQL injection attack and should be avoided at all costs
23rd Mar 2017, 6:49 AM
Akwin Lopez
Akwin Lopez - avatar
0
Your question has ambiguity. What do you mean by SQL? Inline SQL code? What do you mean by procedure? Stored procedure?
21st Mar 2017, 4:12 PM
Satish Gawde
0
What is your Question ? I couldn't understand
22nd Mar 2017, 6:30 AM
Akwin Lopez
Akwin Lopez - avatar
0
Is using SQL code is better than using stored procedures? Please explain why
22nd Mar 2017, 2:33 PM
rohit paul
rohit paul - avatar