Share via


Using new THROW statement

Question

Wednesday, April 25, 2012 7:35 PM | 1 vote

I'm trying to to create a simple stored procedure that uses the new THROW statement instead of the RAISERROR statement. When I enter the THROW statement, I get this syntax error: Incorrect syntax near 'THROW'. Expecting CONVERSATION, DIALOG, DISTRIBUTED, or TRANSACTION. Here's the stored procedure:

CREATE PROC spInsertInvoice_Throw
       @VendorID    int,           @InvoiceNumber  varchar(50),
       @InvoiceDate smalldatetime, @InvoiceTotal   money,
       @TermsID     int,           @InvoiceDueDate smalldatetime
AS
IF EXISTS(SELECT * FROM Vendors WHERE VendorID = @VendorID)
    BEGIN
        INSERT Invoices
        VALUES (@VendorID, @InvoiceNumber,
                @InvoiceDate, @InvoiceTotal, 0, 0,
                @TermsID, @InvoiceDueDate, NULL)
    END
ELSE 
    BEGIN
        THROW 50001, 'Not a valid VendorID!', 1
    END

Can anyone tell me what I'm doing wrong?

All replies (4)

Wednesday, April 25, 2012 8:20 PM ✅Answered | 6 votes

Help says that the previous statement must end with the semicolon. Also, are you sure you're using SQL Server 2012?

This worked for me:

ALTER PROC spInsertInvoice_Throw
       @VendorID    int,           
      @Full_name varchar(100)
AS
IF NOT EXISTS(SELECT * FROM i_Vendor WHERE Vendor_ID = @VendorID)
    BEGIN 
        INSERT i_vendor (Full_name)
        VALUES (@Full_name);
    END
ELSE 
    BEGIN
        ;THROW 50001, 'Not a valid VendorID!', 1
    END

    GO

Notice, that I added ; before throw.

For every expert, there is an equal and opposite expert. - Becker's Law

My blog


Wednesday, April 25, 2012 8:57 PM

Thanks, Naomi. The semicolon before THROW did the trick. I guess you have to code it even if there isn't a previous statement in the block. Very odd.


Monday, May 7, 2012 2:37 AM

 use ; after the first Begin..End statement.


Tuesday, March 11, 2014 12:55 PM

I had the same issue, and it turns out I was using SQL Server 2008.

FYI for other non-DBAs

SELECT @@VERSION;

Gives you vesion info!