0

I am trying to isolate an issue (that resembles the topic in stackoverflow.com/q/483787/537284). The issue involves a stored procedure and occurs "randomly" once a week. To try and reproduce the issue, I created different procedure versions to mimic good and bad possible outcomes:

  • Good multi-row single resultset.
  • Good multi-row single resultset with informational message.
  • Good multi-row single resultset with Raiserror (less than level 11).
  • Good multi-row single resultset with Print.
  • Good empty single resultset.
  • Bad syntax error.
  • Bad syntax error with try/catch.
  • Bad error with Raiserror (level 11).
  • Bad error with Raiserror (level 11) and try/catch.

Between these tests, the syntax error with try/catch version behaved differently than what I would expect. Two resultsets (one empty and the other from the catch instruction) come back.

Does the syntax error get partially executed? I expected the catch block's result and not the try. I compared this with a Raiserror and using severity 11, it triggers the catch block with only one resultset returned. What is the difference between the syntax error and the Raiserror?

Here is my test procedure:

AS
BEGIN
    SET NOCOUNT ON
    SET ANSI_WARNINGS ON    
    SET IMPLICIT_TRANSACTIONS OFF 
    SET XACT_ABORT OFF 
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

BEGIN TRY
    --RAISERROR ('goes to message tab yes?', 11, 1) WITH NOWAIT
    SELECT '1' [myfield] FROM test_fulltext (nolock) WHERE CONTAINS(Command,'a monkey')
    RETURN 0
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER() [ErrorNumber]
END CATCH
RETURN -9999
END

Here is my test table (ripped from others):

CREATE TABLE test_fulltext 
( 
    SPID INT NOT NULL, 
    Status VARCHAR(32) NULL, 
    Login SYSNAME NULL, 
    HostName SYSNAME NULL, 
    BlkBy SYSNAME NULL, 
    DBName SYSNAME NULL, 
    Command VARCHAR(32) NULL, 
    CPUTime INT NULL, 
    DiskIO INT NULL, 
    LastBatch VARCHAR(14) NULL, 
    ProgramName VARCHAR(32) NULL, 
    SPID2 INT 
)
CREATE UNIQUE INDEX fulltextui ON test_fulltext(SPID);
CREATE FULLTEXT CATALOG fulltextft AS DEFAULT;
CREATE FULLTEXT INDEX ON test_fulltext(Command) KEY INDEX fulltextui;
杜興怡
  • 135
  • 1
  • 7

1 Answers1

0

Make sure to read the Remarks section fully on TRY...CATCH in Books Online. It explains this behavior in painful detail.

What I don't understand is this whole business about "forcing a syntax error"... If it's in a stored procedure and you have a syntax error in it, the SP won't even be created. Could it be that you're actually executing an older version of the stored procedure?

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
  • Thanks for the article. It is very helpful. It answers the question about the difference between Raiserror and syntax error. I believe the stored procedure compiles and creates because the syntax error is inside the pattern match string which must be getting through at creation time. I will try and post there with my question. There is an explanation about recompile and postponed name resolution that seems to closest, but it still states that the error will not be trapped by the catch and will return to the original execution level. What I am witnessing is the catch executing after syntax err. – 杜興怡 Dec 10 '10 at 19:41
  • This behavior seems to be particular to my environment (2008 SP1) which is not present in SP2. The Books Online article referenced by @DaveMarkle explains the correct behavior. The msdn thread for reference [link](http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/db7d18db-4cac-49c1-befd-c668c0c6b0a0/) – 杜興怡 Feb 10 '11 at 06:13