Good Practices to Write Stored Procedures in SQL Server
Reference to: http://www.c-sharpcorner.com/UploadFile/skumaar_mca/good-practices-to-write-the-stored-procedures-in-sql-server/
- Use proper indentation for the statements in SQL Server. It will improve the readability.
- Write the proper comments between the logics. So the others can understand quickly.
- Write all the SQL Server keywords in the CAPS letter. For example SELECT, FROM and CREATE.
- Write the stored procedure name with full qualified names.
CREATE PROCEDURE [dbo].EmployeeSalaryCalculation
- Always try to declare the DECLARATION and initialization at the beginning of the stored procedure.
- It is not recommended to use more variables in the procedure. It will occupy more space in the memory.
- Do not write the stored procedure name beginning with sp_. It is reserved for the system stored procedures in SQL Server and when the request comes to the SQL Server engine, it will be considerd to be a system stored procedure and looks for it in the master database. After it understands that this is a user defined stored procedure, it requires a bit more response time. So name the procedure name with another prefix such a proc_.
- Set the SET NOCOUNT ON option in the beginning of the stored procedure to avoid the unnecessary message like number of rows affected by the SQL Server.
- Try to avoid the temp table in the stored procedure. Stored procedures usually use a cached execution plan to increase the performance. When you use the temp table it will do the compilation every time.
- Do not use the select all columns (SELECT *) option; use only specific columns to query the result.
- Try to avoid the cursor in the stored procedure. It will consume more memories. It will degrade the performance of the stored procedure. Try to use the table variable and WHILE loop statement to iterate the query result set.
- Set the default value to the parameter and always set the size of the variable to be equivalent to or more than the table field column length. For example Name (10) in the table, but if you give Name(25) in the procedure then you will get the run time error time "string truncated error".
- Use the Try catch statement properly in the stored procedure to handle the errors in the runtime.
- Move the complex query into views.
- If you want to return the single column result then prefer to use the output statement to return the result to the calling program rather than table result.
- Avoid the sub-queries and use the INNER JOIN. Try to avoid the filtering condition in the where clause and it can be written in the joining time itself. When joins the table itself it will be filtered and it will filter again from the joined result table.
- Use the SELECT TOP 1 in the exists condition checking.
- Do not do like this:
SELECT @name=name FROM employees WHERE name like '%rob%'
This will give the run time error when returns more than one result.
SELECT TOP 1 @name=name FROM employees WHERE name like '%rob%'
It is always recommended to use the TOP 1 in that case. The result may differ from what is expected.
- Avoid the nested IF statements and use the CASE statement. It will execute the matching part immediately.
- Dynamic Queries - Try to minimize the usage of dynamic queries. If you are using a dynamic query like:
SELECT * FROM mydb.dbo.emp where empid = @eid then there is no problem.
You can supply a value for the @eid parameter and there is no recompilation of the execution plan in the database cache. But if you are using a SQL query like SELECT * FROM emp where empid = " + @eid and supply a parameter.
- Use the ORDER BY and DISTINCT, TOP only when requires. The SQL Server engine will get the result first and it will do again the query execution for these operations.
- It is recommended to use a Table variable when the result set is small. It is always in the memory and when the limit exceeds it will be created as a table in the temp. But the temp table will be created on the temp database and that makes it slower.
- Use the proper indexing to the columns in the table. Do not create an index on the columns that are not used anywhere in the where clause. It will require an extra roundtrip to query the result.
Good Practices to Write Stored Procedures in SQL Server的更多相关文章
- Part 10 Stored procedures in sql server
Stored procedures in sql server Stored procedures with output parameters Stored procedure output par ...
- Modify a Stored Procedure using SQL Server Management Studio
In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand ...
- SQL Server 2008性能故障排查(二)——CPU
原文:SQL Server 2008性能故障排查(二)--CPU 承接上一篇:SQL Server 2008性能故障排查(一)--概论 说明一下,CSDN的博客编辑非常不人性化,我在word里面都排好 ...
- SQL Server ->> Database Promgramming Object Security Control(数据库编程对象安全控制)
对于SQL Server内编程对象的安全控制是今天我在思考的问题.在MSDN上找到了几篇有用的文章. 首先微软推荐了三种做法: 1)第一种做法是在SQL Server中对一个应用程序对应创建应用程序角 ...
- Microsoft SQL Server Version List [sqlserver 7.0-------sql server 2016]
http://sqlserverbuilds.blogspot.jp/ What version of SQL Server do I have? This unofficial build ch ...
- Microsoft SQL Server Version List(SQL Server 版本)
原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...
- SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configur
参见:http://msdn.microsoft.com/zh-cn/library/ms191188(SQL.105).aspx Ole Automation Procedures 选项 [本主题为 ...
- SQL Server Metadata
http://www.devart.com/dotconnect/sqlserver/docs/MetaData.htmlhttps://msdn.microsoft.com/en-us/librar ...
- SQL Server 2014查看服务器数据库字段报错 (Microsoft.SqlServer.Management.Sdk.Sfc)
报错信息 无法为该请求检索数据. (Microsoft.SqlServer.Management.Sdk.Sfc) 未知属性 IsMemoryOptimized (Microsoft.SqlServe ...
随机推荐
- U盘安装操作系统
U盘安装操作系统 On 2010 年 4 月 27 日, in IT, by hr 写在前面 本文主要介绍如何使用U盘安装系统,无法使用光驱安装或者不想随身带着光盘,使用这招都很管用,而且备着这样一只 ...
- 转自一个CG大神的文章
<如何学好游戏3D引擎编程>此篇文章献给那些为了游戏编程不怕困难的热血青年,它的神秘要我永远不间断的去挑战自我,超越自我,这样才能攀登到游戏技术的最高峰 ——阿哲VS自 ...
- 在AndroidStudio不能找到so文件问题:couldn't find libweibosdkcore.so
解决步骤已经写到我的公众号,二维码在下面. 欢迎观看我的CSDN学院课程,地址:http://edu.csdn.net/course/detail/2877 本人联系方式: 更多精彩分享,可关注我的微 ...
- Python 十进制转二进制代码实现
def my_bin(num): la = [] if num < 0: return '-' + my_bin(abs(num)) while True: num, remainder = d ...
- JSP编译原理图解
- centos6.3环境下升级python及MySQLdb的安装
近来突然想鼓捣下linux下的python,看下Python数据库方面的东西,想着在centos下测试下.然而安装的过程有很多坑.下面对整个流程进行下记录 1.python基本库的安装 在安装pyth ...
- qsort函数、sort函数【转】
http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...
- 安装sql2008的时候选择的是Windows身份验证,现在我想用sql server身份验证,我应该到哪儿设置呢
本文档整理自网络,存粹用作笔记. 安装过程中,SQL Server 数据库引擎可以设置为 Windows 身份验证模式或 SQL Server 和 Windows 身份验证模式. 本文介绍如何在安装后 ...
- java关键字:synchronized
JAVA 如何共享资源 关于synchronized函数: java具有内置机制,可防止某种资源(此处指的是对象的内存内容)冲突.由于你通常会将某class的数据元素声明为private,并且只经由其 ...
- 富文本编辑器防止xss注入javascript版
富文本编辑器:ueditor 其实富文本编辑器已经有防止xss注入功能,但是你服务端程序在接收的时候在做一次转义,否则有可能然后前端验证直接提交数据导致被xss攻击. 为了节省后端程序开销则在前端 显 ...