SQL注释标头示例


67

也想看看人们的存储过程/函数等注释标题是什么样子(因此请发布您的示例)...我只真正看到了SQL Server Management Studio创建的内容,但对其他人的样子很感兴趣...格式,使用的字符,过程信息/细节等,我想是真正使它们与众不同的原因...

SQL Server Management Studio(版本9)存储过程注释标头默认值:

-- =============================================
-- Author:      Name
-- Create date: 
-- Description: 
-- =============================================

7
如果行格式存在问题,请不要使用行注释(-),而应使用块注释(/ * * /)。
加百利·吉马良斯

1
您能详细说明一下@GabrielGuimarães吗?我有很多带有行注释的脚本,以前没有意识到:我应该注意什么?
罗伯特·马克·布拉姆

@GabrielGuimarãesManagement Studio具有一个函数注释为“注释选定的行”,该函数会将行注释附加到所有选定的行。反功能“取消注释选定的行”可从所有选定的行中删除最前面的注释。对我来说效果很好。您有什么行格式问题?
Iain Samuel McLean年长者,

3
如果在“旧SQL Box”上使用sp_helptext过程获取该过程的脚本,则这些行均未中断,并且由于该原因,结果是单行,因此无法知道注释应在何处结束。
加百利·吉马良斯

@GabrielGuimarães+1:行注释使用格式设置可以省去很多麻烦-恕我直言。
ScottWelker

Answers:


21
--
-- STORED PROCEDURE
--     Name of stored procedure.
--
-- DESCRIPTION
--     Business description of the stored procedure's functionality.
--
-- PARAMETERS
--     @InputParameter1
--         * Description of @InputParameter1 and how it is used.
--
-- RETURN VALUE
--         0 - No Error.
--     -1000 - Description of cause of non-zero return value.
--
-- PROGRAMMING NOTES
--     Gotchas and other notes for your fellow programmer.
--
-- CHANGE HISTORY
--     05 May 2009 - Who
--        * More comprehensive description of the change than that included with the
--          source code commit message.
--

14

我知道这篇文章很古老,但是格式正确的代码永远不会过时。

我将所有程序都使用此模板。有些人不喜欢冗长的代码和注释,但是由于某些人经常需要更新自90年代中期以来未曾涉及的存储过程,因此我可以告诉您编写格式良好且注释严重的代码的价值。编写的内容尽可能简明扼要,有时可能需要几天的时间才能掌握该过程的意图。仅通过阅读就可以很容易地看到代码块的功能,但要难得多(有时甚至是不可能)是在没有适当注释的情况下理解代码的意图。

向您解释它,就像您正在经历初级开发人员一样。假设阅读它的人对它要解决的功能领域一无所知,并且对SQL的了解有限。为什么?很多时候,即使他们无意或不愿意对其进行修改,人们也必须查看程序以理解它们。

/***************************************************************************************************
Procedure:          dbo.usp_DoSomeStuff
Create Date:        2018-01-25
Author:             Joe Expert
Description:        Verbose description of what the query does goes here. Be specific and don't be
                    afraid to say too much. More is better, than less, every single time. Think about
                    "what, when, where, how and why" when authoring a description.
Call by:            [schema.usp_ProcThatCallsThis]
                    [Application Name]
                    [Job]
                    [PLC/Interface]
Affected table(s):  [schema.TableModifiedByProc1]
                    [schema.TableModifiedByProc2]
Used By:            Functional Area this is use in, for example, Payroll, Accounting, Finance
Parameter(s):       @param1 - description and usage
                    @param2 - description and usage
Usage:              EXEC dbo.usp_DoSomeStuff
                        @param1 = 1,
                        @param2 = 3,
                        @param3 = 2
                    Additional notes or caveats about this object, like where is can and cannot be run, or
                    gotchas to watch for when using it.
****************************************************************************************************
SUMMARY OF CHANGES
Date(yyyy-mm-dd)    Author              Comments
------------------- ------------------- ------------------------------------------------------------
2012-04-27          John Usdaworkhur    Move Z <-> X was done in a single step. Warehouse does not
                                        allow this. Converted to two step process.
                                        Z <-> 7 <-> X
                                            1) move class Z to class 7
                                            2) move class 7 to class X

2018-03-22          Maan Widaplan       General formatting and added header information.
2018-03-22          Maan Widaplan       Added logic to automatically Move G <-> H after 12 months.
***************************************************************************************************/

除了此标头之外,您的代码还应该经过注释,并从上到下概述。向主要功能部分添加注释块,例如:

/***********************************
**  Process all new Inventory records
**  Verify quantities and mark as
**  available to ship.
************************************/

添加许多内联注释,解释除最基本条件以外的所有条件,并始终设置代码格式以提高可读性。缩进代码的垂直长页面比宽而短的页面好,并且在其他人支持您的代码的情况下,更容易查看代码块的起始位置和结束时间。有时,宽大的,非缩进的代码更具可读性。如果是这样,请仅在必要时使用它。

UPDATE Pallets
SET class_code = 'X'
WHERE
    AND class_code != 'D'
    AND class_code = 'Z' 
    AND historical = 'N'
    AND quantity > 0
    AND GETDATE() > DATEADD(minute, 30, creation_date)
    AND pallet_id IN ( -- Only update pallets that we've created an Adjustment record for
        SELECT Adjust_ID
        FROM Adjustments
        WHERE
            AdjustmentStatus = 0
            AND RecID > @MaxAdjNumber

编辑

我最近放弃了横幅样式注释块,因为随着时间的推移更新代码时,顶部和底部注释很容易分离。您可以在注释块中以逻辑上分开的代码结尾,这些注释块属于同一注释,这会产生比其解决的问题更多的问题。我已经开始用BEGIN ... END块围绕多个语句部分,并将流注释放在每个语句的第一行旁边。这样的好处是,您可以折叠代码块并能够清楚地阅读高级流程注释,并且当您打开一个分支时,您将可以对其中的各个语句执行相同的操作。这也非常适合于高度嵌套的代码级别。当您的处理程序开始爬入200-400行范围并且没有变化时,这是无价的

展开式

在此处输入图片说明

倒塌

在此处输入图片说明


12
-------------------------------------------------------------------------------
-- Author       name
-- Created      date
-- Purpose      description of the business/technical purpose
--              using multiple lines as needed
-- Copyright © yyyy, Company Name, All Rights Reserved
-------------------------------------------------------------------------------
-- Modification History
--
-- 01/01/0000  developer full name  
--      A comprehensive description of the changes. The description may use as 
--      many lines as needed.
-------------------------------------------------------------------------------

10

我们使用这样的东西,对我非常有用。

/*  
Description:   
Author:   
Create Date: 
Param:   
Return:   
Modified Date:  
Modification:   
*/  

6
-- [why did we write this?]
-- [auto-generated change control info]

2
究竟。记录为什么比什么更有价值。尽管是初学者,但我更喜欢为什么,什么以及如何。:)
Thor Hovden 2011年

5
set timing on <br>
set linesize 180<br>
spool template.log

/*<br>
##########################################################################<br>
-- Name : Template.sql<br>
-- Date             : (sysdate) <br>
-- Author           :   Duncan van der Zalm - dvdzalm<br>
-- Company          :   stanDaarD-Z.nl<br>
-- Purpose          :   <br>
-- Usage        sqlplus <br>
-- Impact   :<br>
-- Required grants  :   sel on A, upd on B, drop on C<br>
-- Called by        :   some other process<br
##########################################################################<br>
-- ver  user    date        change  <br>
-- 1.0  DDZ 20110622    initial<br>
##########################################################################<br>
*/<br>

sho user<br>

select name from v$database;

select to_char(sysdate, 'Day DD Month yyyy HH24:MI:SS') "Start time"
from dual
;


-- script


select to_char(sysdate, 'Day DD Month yyyy HH24:MI:SS') "End time"
from dual
;

spool off

3

我们当前使用的标头如下所示:

---------------------------------------------------
-- Produced By   : Our company  
-- URL       : www.company.com  
-- Author        : me   
-- Date      : yesterday    
-- Purpose       : to do something  
-- Called by     : some other process   
-- Modifications : some other guy - today - to fix my bug   
------------------------------------------------------------

附带说明一下,我在SQL中放置的所有注释始终使用以下格式:

/ *评论* /

像过去一样,我遇到问题,即(通过SQL Server)脚本会做一些有趣的事情,换行并以注释开头-已经注释掉了必需的SQL ....但是那可能就是我。


10
“日期:昨天”,大声笑
Jeffrey Kemp

1

查看这是否符合您的要求:

/*  

* Notes on parameters: Give the details of all parameters supplied to the proc  

* This procedure will perform the following tasks: 
 Give details description of the intent of the proc  

* Additional notes: 
Give information of something that you think needs additional mention, though is not directly related to the proc  

* Modification History:
  07/11/2001    ACL    TICKET/BUGID        CHANGE DESCRIPTION


*/


0

这是我目前使用的。三元注释(/ * / * / *)用于从对象定义中挑选标题注释的集成。

/*/*/*

    Name:           pr_ProcName
    Author:         Joe Smith
    Written:        6/15/16
    Purpose:        Short description about the proc.

    Edit History:   6/15/16 - Joe Smith
                        + Initial creation.
                    6/22/16 - Jaden Smith
                        + Change source to blahblah
                        + Optimized JOIN
                    6/30/16 - Joe Smith
                        + Reverted changes made by Jaden.

*/*/*/

这是哪个集成?现成的或定制的?
unfinishedmonkey

@unfinishedmonkey自定义-我构建了一个过程,该过程扫描对象的模式并将注释存储在表中。
chazbot7

0

这是我的首选变体:

/* =====================================================================
DESC:   Some notes about what this does
           tabbed in if you need additional lines
NOTES:  Additional notes
           tabbed in if you need additional lines
======================================================================== */
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.