I am facing the famous 'Incorrect syntax' while using a THROW
statement in a T-SQL stored procedure. I have Googled it and checked the questions on StackOverflow but the solutions proposed (and strangely, accepted) do not work for me.
I am modifying a stored procedure as follows:
ALTER PROCEDURE [dbo].[CONVERT_Q_TO_O] @Q_ID int = NULL, @IDENTITY INT = NULL OUTPUT AS BEGIN SET NOCOUNT ON; DECLARE @EXISTING_RECORD_COUNT [int]; SELECT @EXISTING_RECORD_COUNT = COUNT (*) FROM [dbo].[O] WHERE [Q_ID] = @Q_ID IF @EXISTING_RECORD_COUNT = 0 BEGIN -- DO SOME STUFF HERE -- RETURN NEW ID SELECT @IDENTITY = SCOPE_IDENTITY() END ELSE BEGIN THROW 99001, 'O associated with the given Q Id already exists', 1; END END GO
When I code this T-SQL I get an error saying
Incorrect statement near 'THROW'. Expecting CONVERSATION, DIALOG, DISTRIBUTED, or TRANSACTION
All solutions suggest to put a semi-colon either before 'THROW' or after 'ELSE BEGIN' statements. When I modify the T-SQL I simply get the "Incorrect statement near 'THROW'" error and can't seem to find a solution.
Any suggestions?
The following example shows how to use the THROW statement to raise the last thrown exception again. USE tempdb; GO CREATE TABLE dbo. TestRethrow ( ID INT PRIMARY KEY ); BEGIN TRY INSERT dbo. TestRethrow(ID) VALUES(1); -- Force error 2627, Violation of PRIMARY KEY constraint to be raised.
SQL Server provides TRY, CATCH blocks for exception handling. We can put all T-SQL statements into a TRY BLOCK and the code for exception handling can be put into a CATCH block. We can also generate user-defined errors using a THROW block.
RAISERROR is a SQL Server error handling statement that generates an error message and initiates error processing. RAISERROR can either reference a user-defined message that is stored in the sys. messages catalog view or it can build a message dynamically.
This continues to occur in SQL Server 2014.
I have found that putting the semi-colon at the end of BEGIN helps.
This approach has the error
IF 'A'='A' BEGIN THROW 51000, 'ERROR', 1; END;
And this approach does not have the error
IF 'A'='A' BEGIN; THROW 51000, 'ERROR', 1; END;
To solve your problem,
Incorrect statement near 'THROW'. Expecting CONVERSATION, DIALOG, DISTRIBUTED, or TRANSACTION
put semi-colon before your throw statement:
BEGIN ;THROW 99001, 'O associated with the given Q Id already exists', 1; END
And about the
"Incorrect statement near 'THROW'".
Try to use this in case you're using a older version than SQL 2012:
RAISERROR('O associated with the given Q Id already exists',16,1);
Because THROW is a new feature of SQL 2012.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With