A simple trick for “Block Comment” syntax in SSMS

  • zerko

    SSCommitted

    Points: 1524

    Comments posted to this topic are about the item A simple trick for “Block Comment” syntax in SSMS

  • reuben.anderson

    SSC Eights!

    Points: 816

    Oh, I love this stuff.

    Thanks Zerko.

  • Carlo Romagnano

    SSC-Insane

    Points: 21987

    Wonderfull!

    You gave me a good Idea.

    😀

  • Johan Bijnens

    SSC Guru

    Points: 134286

    nice and simple trick.

    Thank you for sharing.

    Johan


    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt ?

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me[/url] :alien: but most of the time this is me :hehe:

  • Yiremyahu

    SSCrazy

    Points: 2104

    I use comments to test changes to parameterised stored procedures without having to create or alter the sproc first.

    /**/

    CREATE PROC Test @param1 INT, @param2 VARCHAR(20) AS

    --*/DECLARE @param1 INT/*

    --*/DECLARE @param2 VARCHAR(20)/*

    --*/

    SELECT @param1, @param2

    By breaking the close comment in the initial /**/ you coment out the CREATE (or ALTER) statement and uncomment the DECLARE statements.

    /**-/

    CREATE PROC Test @param1 INT, @param2 VARCHAR(20) AS

    --*/DECLARE @param1 INT/*

    --*/DECLARE @param2 VARCHAR(20)/*

    --*/

    SELECT @param1, @param2

  • Phil Parkin

    SSC Guru

    Points: 244662

    This is exactly the technique I use in long 'utility' scripts and it works very well. I generally leave everything commented out until I need it, in case someone drops something on my F5 key :crazy:

    There is one additional thing I would like to be able to do though: select a block of text and then use a keyboard shortcut (like CTRL-K/CTRL-C) to automatically insert the /*[textblock]*/ around the block (/*[textblock]--*/ would be even better!).

    I wonder if anyone has tried to do this?

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help/ for details of how to post T-SQL code-related questions.

  • shoestringdba

    SSCertifiable

    Points: 6206

    Neat trick.

    There is one additional thing I would like to be able to do though: select a block of text and then use a keyboard shortcut (like CTRL-K/CTRL-C) to automatically insert the /*[textblock]*/ around the block (/*[textblock]--*/ would be even better!).

    I wonder if anyone has tried to do this?

    All the built-in ways I could find on the various menus and such in SSMS (including stuff not shown by default) seem to use the double-dash type comment. I have heard about third-party add-ins that add macro capability to SSMS that might prove useful.

    ____________
    Just my $0.02 from over here in the cheap seats of the peanut gallery - please adjust for inflation and/or your local currency.

  • Tee Time

    Hall of Fame

    Points: 3693

    Thanks zerko!

  • Alex Friedman

    Right there with Babe

    Points: 755

    lshanahan (7/8/2013)


    Neat trick.

    There is one additional thing I would like to be able to do though: select a block of text and then use a keyboard shortcut (like CTRL-K/CTRL-C) to automatically insert the /*[textblock]*/ around the block (/*[textblock]--*/ would be even better!).

    I wonder if anyone has tried to do this?

    All the built-in ways I could find on the various menus and such in SSMS (including stuff not shown by default) seem to use the double-dash type comment. I have heard about third-party add-ins that add macro capability to SSMS that might prove useful.

    The SSMSBoost add-in does it (along with some other neat stuff).

  • the sqlist

    SSCrazy

    Points: 2398

    How about the comment toggle trick:

    -- Toggle trick

    SELECT 'Toggle line 1' /*

    SELECT 'Toggle line 2' --*/

    By commenting line 1 line 2 becomes active.

    Don't just give the hungry man a fish, teach him how to catch it as well.

    the sqlist

  • Mike Dougherty-384281

    SSCrazy

    Points: 2764

    I hope I never have to inherit any of these scripts. It isn't exactly 'self documenting' to have nested commented code blocks that sometimes should run and other times should not run.

    For all the clever that went into manually comment in/out of code, you could probably have written a proper IF block. ex: IF !EXISTS(/* check for table */) begin /* create table, populate with data, etc. */ end <-- now instead of commenting, you can simply run & rerun the script without a comment block to explain when the comment block should or should not be [un]commented

    If you have any more dynamic manual craziness, don't stop at a clever commenting process: make it a working Stored Procedure. I don't have time to learn this 'convention' for commenting and manually running code. (who does?)

    That said, if you are talking about the process of developing/testing ad-hoc code that is en-route to becoming a real procedure... then thanks for the insight into your methodology. I feel like it is a bit too complicated for me to remember and use naturally.

    btw, thanks for writing the article too.

  • terry 34156

    Newbie

    Points: 8

    I use a slight variation:

    /* Explantory comment *

    Code

    --*/

    Note the asterisk at the end of the comment.

    I can follow that with a slash, and the code

    becomes effective, retaining the comment.

  • Darren Y

    Grasshopper

    Points: 11

    Very useful. I bet I do this all the time from now on.

  • Leon Page

    SSC Rookie

    Points: 44

    Very useful tip. Thanks!

  • Dave Vroman

    SSC Eights!

    Points: 821

    Superb development tool! I'd hate to need to work around this in live code though.

Viewing 15 posts - 1 through 15 (of 54 total)

You must be logged in to reply to this topic. Login to reply