我正在尝试存储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行会出现上述错误。
答案 0 :(得分:3)
您无法使用变量来设置NOCOUNT
:
/* line 23 */ SET NOCOUNT @NCStat ; /* not legal syntax */
我会这样做:
/* line 23 */
if @NCStat = 1
SET NOCOUNT ON
else
SET NOCOUNT OFF