Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Dynamic SQL Server connections in Reporting ServicesIntroducing SQL Server to Oracle »
    comments

    If you have multiple queries in one SQL query file, and you are switching around between which ones you want to run at any one time, there is an easy way to do this.

    The scenario I'm thinking of is something like when you're solving a SQL puzzle/challenge and keep trying new ideas and query versions. You don't want to delete your old queries because they are useful for reference or to copy and paste parts of them, but you don't want to run them all the time. Especially during performance tweaking, you want to be able to quickly comment out the worst queries and keep pasting in new versions of the fastest queries.

    Normally this would require either highlighting entire queries to comment/uncomment them (Ctrl-Shift-C/Ctrl-Shift-U in Query Analyzer or Ctrl-K:Ctrl-C/Ctrl-K:Ctrl-U in SSMS); adding or removing /* and */ at the top and bottom of the query; or highlighting just the portions of the code that you want to run each time, which is a pain and can be complicated if you are using table variables since your initialization code at the top has to run each time.

    So try this!

    Put --/* at the top of your query and --*/ at the bottom:

    1. --/* Test Query #3 (Gorp Method)
    2. SELECT *
    3. FROM Blah
    4. WHERE Gorp = 1
    5. --*/

    Notice that the query is NOT commented out.

    Now, to toggle the whole query as commented or not, just uncomment the first line:

    1. /* Test Query #3 (Gorp Method)
    2. SELECT *
    3. FROM Blah
    4. WHERE Gorp = 1
    5. --*/

    I just hit on this method yesterday and I'm really enjoying it. It provides a natural place to put a query comment, you only have to put the block comments in once, and then you can use your comment/uncomment shortcuts on just a single line to toggle an entire block of code! Just keep in mind that the action IS reversed: you comment to uncomment and you uncomment to comment.

    About the Author

    Erik has been working in IT since 1993 and starting in 2004 has specialized in MS SQL Server query writing, database design, and reporting services. He also professionally does web site design/developing and writes C# applications (mostly for projects involving his databases and web sites). His career in the industry truly started when he first began using computers in the late 70s. In 1984 he began programming on the IBM PCJr, and performed game testing on King's Quest I. These early experiences gave him the taste and drive to continue an incremental and self-taught path all the way to his current position. Erik is also interested in taekwondo, go (ranks 4k on KGS), sci-fi books, mathematics, philosophy, religion, and rollerblading. He and his family live on the West Coast of the US.
    Social SitingsLTD RSS Feed
    2339 views
    Instapaper

    4 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis Neat way.....of course only you could have come up with this :-)

    This is one of the reasons I always use #temp tables instead of variable.....so that I can just run the stuff I need

    I always use CTRL + C/ CTRL + R (I used 2000 setup in SSMS)

    12/12/09 @ 12:21
    Comment from: gitmo [Visitor]
    gitmo Clever.
    12/14/09 @ 14:18
    Comment from: mike good [Visitor]
    mike good I like this trick, never thought of it. But even more, I like "2000 setup in SSMS" -- never heard of that either. 30sec after reading this post, my SSMS now runs with the "SQL 2000" keyboard scheme and I'm back to using Ctrl-C and Ctrl-R. My thanks to both of you.
    12/15/09 @ 13:14
    Comment from: Erik [Member] Email
    Erik I sure miss Ctrl-B ... to move the horizontal splitter bar between the code and the query results up and down with the keyboard. :( :( :(
    12/15/09 @ 17:18

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)