Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8143335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:05:47+00:00 2026-06-06T13:05:47+00:00

Is there a way to create simple unit tests for SQL queries in SSMS?

  • 0

Is there a way to create simple unit tests for SQL queries in SSMS? I’m fairly new to T-SQL & SSMS, but I’d like to try to carry some of my old TDD habits into this arena, if possible.

So, for example, when I write the DDL to create complex functions and what-not, I’d like to have some way to include a unit test that (upon failure) would actually cause the output panel to display an error message upon execution. (This would be analagous to “breaking the build” in a normal programming environment.)

Currently, when I write a function that is syntactically correct, then hit F5 to execute it, the output message is:

Command(s) completed successfully.

What I am looking for is some simple SQL trick to simulate basic red-light/green-light test cases. So I would write a series of test-statements that would pass only if my user-defined function is operating as intended. Otherwise an error message would be displayed, such as:

Msg 207, Level 16, State 1, Line 2
Invalid statement.

which would allow me to jump immediately to the failing test and see what I did wrong.

I don’t expect there to be anything “built-in” for this, but is there some way I can “fake it”?

Update: I just learned you can throw exceptions in SS2012, which I’m sure I could use for just this purpose, but unfortunately I’m stuck with SS2008 for now. Is there anything comparable in SS2008?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:05:48+00:00Added an answer on June 6, 2026 at 1:05 pm

    These are the 2 frameworks I can recomend

    T.S.T.

    http://tst.codeplex.com/

    Testing SQL Server Code with TST

    http://www.infoq.com/articles/tst-sql-server

    tSQLt

    http://tsqlt.org/

    SQL Test (runner for tSQLt)

    http://www.red-gate.com/products/sql-development/sql-test/

    Update 1

    Reading your answer maybe you find the following dump useful.

    TRY CATCH was introduced with SQL Server 2005 (and for this reason one should never look at someting older then 2005). You can actualy (re)throw an exception using a stored procedure mentioned in my dump, including the line number. In SQL Server 2012 they (finaly!) introduced throw as you mention makeing Tsql a robust language after 14 years.

    So here is my dump, I need to clean it up some day so Its more copy paste friendly.

    SET XACT_ABORT ON
    SET CONCAT_NULL_YIELDS_NULL OFF
    
    DECLARE @message varchar ( max ) 
    DECLARE @who varchar ( 255 ) 
    set @who = OBJECT_NAME(@@PROCID) -- name of the currently executing sproc
    
    BEGIN TRY
    
    -- ======================================================================================
    SET @message = 'HELLO'
    EXEC Log @who, @message
    
             ....
    
    -- ======================================================================================
    SET @message = 'GOODBYE'
    EXEC Log @who, @message
    
    END TRY
    
    BEGIN CATCH
    
    
            -- ======================================================================================
    --If an error generated in a TRY block causes the state of the current transaction to be invalidated, the transaction is classified as an uncommittable transaction.
    --An error that ordinarily ends a transaction outside a TRY block causes a transaction to enter an uncommittable state when the error occurs inside a TRY block.
    -- http://msdn.microsoft.com/en-us/library/ms175976.aspx
           if XACT_STATE() = -1 rollback;
    
        -- ======================================================================================
    SET @message = 'Rolling Back transaction if present'
    EXEC Log @who, @message
    
         -- Its important to rollback the transaction at the very start of the catch.
    -- Otherwise the records that are written to the log will also be roll backed.
    
    IF @@TRANCOUNT > 0 
    BEGIN
    ROLLBACK TRAN 
    END
    
    -- ======================================================================================
    SET @message = 'Error Occured '
    set @message = @message + ' ERROR_NUMBER() : ' + cast(ERROR_NUMBER() as varchar(max))
    set @message = @message + ' ERROR_SEVERITY() : ' + cast(ERROR_SEVERITY() as varchar(max))
    set @message = @message + ' ERROR_STATE() : ' + cast(ERROR_STATE() as varchar(max))
    set @message = @message + ' ERROR_PROCEDURE() : ' +cast(ERROR_PROCEDURE() as varchar(max))
    set @message = @message + ' ERROR_LINE() : ' + cast(ERROR_LINE() as varchar(max))
    set @message = @message + ' ERROR_MESSAGE() : ' + cast(ERROR_MESSAGE() as varchar(max))
    
    EXEC Log @who, @message
    
      exec usp_RethrowError
    
    
    END CATCH
    
    
    Error logging sproc and table
    
    CREATE PROCEDURE [dbo].[Log]
    (
    @who varchar(255),
    @message varchar(max)
    )
    AS
    
    SET XACT_ABORT ON
    SET CONCAT_NULL_YIELDS_NULL OFF
    
    INSERT INTO [ApplicationLog]
    (
    [Date],
    [Level],
    [Logger],
    [Host],
    [Message]
    )
    VALUES
    (
    getDate(),
    'INFO',
    @who,
    'dummy',
    @message
    )
    
    CREATE TABLE [dbo].[ApplicationLog] (
    [Id]            [int] IDENTITY(1, 1) NOT NULL,
    [Date]          [datetime] NOT NULL,
    [Thread]        [varchar](255)  NULL,
    [Level]         [varchar](50) NOT NULL,
    [Logger]        [varchar](255)  NOT NULL,
    [Host]          [varchar](50)  NOT NULL,
    [Message]       [varchar](max)  NOT NULL,
    [Exception]     [varchar](max) NULL
    )
    
    
    Rethrow an exception
    
    ALTER PROCEDURE [dbo].[usp_RethrowError]
     -- BOL contains a good example of that, there is a stored procedure called usp_RethrowError
    
    AS -- Return if there is no error information to retrieve.
    
    SET XACT_ABORT ON
    SET CONCAT_NULL_YIELDS_NULL OFF
    
    IF ERROR_NUMBER() IS NULL 
      RETURN ;
    
    DECLARE @ErrorMessage NVARCHAR(4000),
      @ErrorNumber INT,
      @ErrorSeverity INT,
      @ErrorState INT,
      @ErrorLine INT,
      @ErrorProcedure NVARCHAR(200) ;
    
        -- Assign variables to error-handling functions that 
        -- capture information for RAISERROR.
    SELECT  @ErrorNumber = ERROR_NUMBER(), @ErrorSeverity = ERROR_SEVERITY(),
            @ErrorState = ERROR_STATE(), @ErrorLine = ERROR_LINE(),
            @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-') ;
    
        -- Building the message string that will contain original
        -- error information.
    SELECT  @ErrorMessage = N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' +
            'Message: ' + ERROR_MESSAGE() ;
    
        -- Raise an error: msg_str parameter of RAISERROR will contain
        -- the original error information.
    RAISERROR (@ErrorMessage, @ErrorSeverity, 1, @ErrorNumber, -- parameter: original error number.
      @ErrorSeverity, -- parameter: original error severity.
      @ErrorState, -- parameter: original error state.
      @ErrorProcedure, -- parameter: original error procedure name.
      @ErrorLine-- parameter: original error line number.
            ) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to create a new NSString from a format string like
Is there any simple way to create instance of modal DialogBox with single text
Is there a simple way to create a valid icalendar file for a single
I'd like to know if there is a simple (or already created) way of
Is there any way to create a property like this C# property in Objective-C?
Is there any way to create a simple timeline of events with Google Charts?
Is there any simple way to create a GUI using OpenCV library or linking
Simple and short question: Is there a way to create a handler for custom
I'm trying to run some unit tests using QUnit written in CoffeeScript but there
To phrase my question as simply as possible, is there a way to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.