2

我正在尝试存储 NOCOUNT 状态,以便在我的过程结束时将其恢复到原始状态,但它所做的只是给我一个Incorrect syntax near 'NOCOUNT'.错误。

我究竟做错了什么?

IF @@OPTIONS & 512 <> 0     /* check original state of NOCOUNT */
      PRINT N'This user has SET NOCOUNT turned ON.';
  ELSE
      PRINT N'This user has SET NOCOUNT turned OFF.';

DECLARE @NCStat bit
    SET @NCStat = ( @@OPTIONS & 512 )   /* sets @NCStat to original state of NOCOUNT */

SET NOCOUNT ON ;

IF @@OPTIONS & 512 <> 0     /* verify state of NOCOUNT */
      PRINT N'This user has SET NOCOUNT turned ON.';
  ELSE
      PRINT N'This user has SET NOCOUNT turned OFF.';

PRINT N'NCStat = ' + cast(@NCStat as nvarchar) ;        /* verify value of @NCStat */

/* line 23 */  SET NOCOUNT  @NCStat ;       /* return NOCOUNT to original state */

IF @@OPTIONS & 512 <> 0     /* verify state of NOCOUNT */
      PRINT N'This user has SET NOCOUNT turned ON.';
  ELSE
      PRINT N'This user has SET NOCOUNT turned OFF.';
GO

如果注释掉第 23 行,则所有其他行都可以正常工作,但第 23 行给出了上述错误。

4

1 回答 1

3

您不能使用变量来设置NOCOUNT

/* line 23 */  SET NOCOUNT  @NCStat ; /* not legal syntax */

我会这样做:

/* line 23 */
if @NCStat = 1
    SET NOCOUNT ON
else
    SET NOCOUNT OFF
于 2013-03-17T23:56:39.710 回答