Reference to: http://www.c-sharpcorner.com/UploadFile/skumaar_mca/good-practices-to-write-the-stored-procedures-in-sql-server/

    1. Use proper indentation for the statements in SQL Server. It will improve the readability.
    2. Write the proper comments between the logics. So the others can understand quickly.
    3. Write all the SQL Server keywords in the CAPS letter. For example SELECT, FROM and CREATE.
    4. Write the stored procedure name with full qualified names.

      CREATE PROCEDURE [dbo].EmployeeSalaryCalculation

    5. Always try to declare the DECLARATION and initialization at the beginning of the stored procedure.
    6. It is not recommended to use more variables in the procedure. It will occupy more space in the memory.
    7. 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_.
    8. 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.
    9. 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.
    10. Do not use the select all columns (SELECT *) option; use only specific columns to query the result.
    11. 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.
    12. 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".
    13. Use the Try catch statement properly in the stored procedure to handle the errors in the runtime.
    14. Move the complex query into views.
    15. 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.
    16. 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.
    17. Use the SELECT TOP 1 in the exists condition checking.
    18. 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.

    19. Avoid the nested IF statements and use the CASE statement. It will execute the matching part immediately.
    20. 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.

    21. 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.
    22. 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.
    23. 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的更多相关文章

  1. Part 10 Stored procedures in sql server

    Stored procedures in sql server Stored procedures with output parameters Stored procedure output par ...

  2. 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  ...

  3. SQL Server 2008性能故障排查(二)——CPU

    原文:SQL Server 2008性能故障排查(二)--CPU 承接上一篇:SQL Server 2008性能故障排查(一)--概论 说明一下,CSDN的博客编辑非常不人性化,我在word里面都排好 ...

  4. SQL Server ->> Database Promgramming Object Security Control(数据库编程对象安全控制)

    对于SQL Server内编程对象的安全控制是今天我在思考的问题.在MSDN上找到了几篇有用的文章. 首先微软推荐了三种做法: 1)第一种做法是在SQL Server中对一个应用程序对应创建应用程序角 ...

  5. 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 ...

  6. 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 ...

  7. SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configur

    参见:http://msdn.microsoft.com/zh-cn/library/ms191188(SQL.105).aspx Ole Automation Procedures 选项 [本主题为 ...

  8. SQL Server Metadata

    http://www.devart.com/dotconnect/sqlserver/docs/MetaData.htmlhttps://msdn.microsoft.com/en-us/librar ...

  9. SQL Server 2014查看服务器数据库字段报错 (Microsoft.SqlServer.Management.Sdk.Sfc)

    报错信息 无法为该请求检索数据. (Microsoft.SqlServer.Management.Sdk.Sfc) 未知属性 IsMemoryOptimized (Microsoft.SqlServe ...

随机推荐

  1. JavaScript变量作用域

    全部变量拥有全局作用域,局部变量拥有局部作用域(这里注意函数的参数也是局部变量) 1.在函数体内,局部变量的优先级高于同名的全局变量. 我的理解就是当你同时定义了同名的局部变量和全局变量时,函数体内返 ...

  2. Tomcat调试笔记

    调试笔记 在使用Tomcat过程中经常碰到问题,导致tomcat启动失败.如下↓ 由于报错太过笼统,我根本无法找出错误.后来我切换到Console视图下,看到了如下错误信息. 根据报错信息,错误原因是 ...

  3. ASP.NET Razor - C# 变量

    变量是用来存储数据的命名实体. 变量 变量是用来存储数据的. 一个变量的名称必须以字母字符开头,并且不能包含空格或者保留字符. 一个变量可以是一个指定的类型,表示它所存储的数据类型.string 变量 ...

  4. Kali2.0VMwareTools安装

    安装完系统后,配置好apt源,然后执行下列语句可成功安装VMwareTools工具,可实现本机与虚拟机之间的文件复制等功能: apt-get install open-vm-tools-desktop ...

  5. IPv6实验准备

    这篇是我的第一篇博客,我想先对H3C的<IPv6技术>的实验部分进行实验和总结,欢迎评论转载. 本实验用的网路设备模拟器是HCL_7.1.59,hcl的这款模拟器非常耗费内存,各种报错,因 ...

  6. 流编辑器-sed

    sed 参数: 1.'s' 替换 sed 's/search-word/replace-word/' file-name 替换file-name文件中的search-word为replace-word ...

  7. Apache-Jemeter web性能测试工具使用

    Jmeter是一款java开源的性能测试软件. 要使用该工具进行性能测试,首先需要下载该工具到你的电脑,接着配置java开发环境以及Jmeter环境.搭建完成之后,OK,我们就可以进行测试了. 测试第 ...

  8. OpenGL法向量变换

    OpenGL光照开启时,法向量用于决定特定顶点或面上接受到光照的多少.光照处理过程作用于观察坐标空间,因此,模型对象坐标系的法向量也需要使用GL_MODELVIEW矩阵变换到观察坐标系. 然而,法向量 ...

  9. div模态层示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 如何在IDEA 中使用Git

    1,下载最新的 git 包 地址: https://git-scm.com/download/win 下载便携版 64,32 根据个人爱好   2,解压后随便放个位置即可,例如图: (不太建议使用它自 ...