Click here to Skip to main content
12,452,000 members (24,861 online)
Click here to Skip to main content
Add your own
alternative version

Stats

3.3K views
2 bookmarked
Posted

Quick Safe SQL Snippet

, 9 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
SQL 2012 Code Snippet for quick blank transaction

Introduction

This snippet creates a blank safe transaction in which to perform database CRUD operations.

SQL 2012 Managment Studio only :( 

Background  

I frequently find myself needing to update database table values in databases which shouldn't really be manipulated manually, but sometimes need to be.

Using the code 

Save this code in a file called "SafeSQLSnippet.snippet" (or whatever you want) in C:\Users\USERNAME\Documents\SQL Server Management Studio\Code Snippets\SQL\My Code Snippets.

<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>SafeSQL Template</Title>
            <Author>Ken Ehrman</Author>
            <Description>Inserts a TRAN-TRY-CATCH.</Description>
            <Shortcut>SafeSQL</Shortcut>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Code Language="SQL">
                <![CDATA[
/* SAFE SQL */
BEGIN TRAN
BEGIN TRY
    /* PUT USEFUL STUFF HERE */
    
    /* WHEN YOU FEEL SAFE, CHANGE THIS TO COMMIT TRAN */
    ROLLBACK TRAN
END TRY
BEGIN CATCH        
    ROLLBACK TRAN
    PRINT 'AT LINE: ' + CAST(ERROR_LINE() AS varchar(10));
    PRINT ERROR_MESSAGE();
END CATCH 

]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Points of Interest 

Should serve as a sort-of tutorial for creating other snippets.

History 

Got tired of writing this code over and over, or remembering were to copy/paste it from.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

groggyjava
Web Developer
United States United States
i've been a computer programmer for a long time. i've been a human even longer. i've had a lot of fun along the way and hope to continue to do so.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 9 Aug 2013
Article Copyright 2013 by groggyjava
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid