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
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.