Search code examples
sqlerror-handlingsql-server-2016raiserror

SQL RAISERROR under CASE


I'm trying to use a RAISERROR in my query but I can't get it working. No doubt I'm trying to do this completely the wrong way.

For example, in the code below I would like it to terminate the query if the DestType field is blank or NULL.

SELECT TOP 1
    DocEntry,
    DocNum,
    CAST(DocDate AS DATE) AS [DocDate],
    CAST(DocDueDate AS date) AS [ShipDate],
    Comments,
    DestType
FROM ....

I tried putting it in a CASE WHEN IN type thing but that didn't work.

Many thanks!


Solution

  • RAISERROR is a statement which cannot be inlined.

    With SQL-Server 2016 (as tagged) you can use

    BEGIN TRY
        Do Something here
    END TRY
    BEGIN CATCH
        RAISERROR ...parameters...
    END CATCH