How to initiate a THROW on a stored procedure if there are 0 results?

I was wondering what I would have to do to throw an exception on the following stored procedure if there were 0 results so it could select the NULL row? This is for MSSQL, thanks so much.

...

BEGIN TRY
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT
        NoteId,
        Url,
        Subject,
        Content,
        ExpiresAt,
        RemindAt
    FROM dbo.browsingnotes_Notes
    WHERE UserId = @UserId
        AND Hash = @Hash
END TRY
BEGIN CATCH
    SELECT
        NULL AS 'NoteId',
        NULL AS 'Url',
        NULL AS 'Subject',
        NULL AS 'Content',
        NULL AS 'ExpiresAt',
        NULL AS 'RemindAt'
END CATCH

Answers


Does RAISERROR work? Or, as @Candie notes, THROW in SQL 2012

You could check @@RowCount after the first select, and if 0, select the second.


Need Your Help

jQuery - getting table rows following a filter match

javascript jquery table filter rows

I have an HTML table that I'm filtering with jQuery, in order to remove rows based on the content of a cell with a certain class. The following code works for that:

Matching pattern containing parentheses with sed

regex bash sed

I need to insert '--' at the beginning of the line if line contains word VARCHAR(1000)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.