SET NOEXEC (Transact-SQL)

编译每个查询但不执行查询。

主题链接图标Transact-SQL 语法约定

语法

SET NOEXEC { ON | OFF }

注释

当 SET NOEXEC 为 ON 时,SQL Server 将编译每一批处理 Transact-SQL 语句但并不执行它们。当 SET NOEXEC 设置为 OFF 时,所有批处理将在编译后执行。

SQL Server 中的语句执行包含两个阶段:编译和执行。该设置可用于让 SQL Server 在执行 Transact-SQL 代码时,验证代码中的语法和对象名。它也可以用于调试通常是较大的批处理中的部分语句。

SET NOEXEC 设置是在执行或运行时设置,而不是在分析时设置。

权限

要求具有 public 角色的成员资格。

示例

下面的示例在有效查询、含有无效对象名的查询以及含有不正确语法的查询中使用 NOEXEC。

USE AdventureWorks;
GO
PRINT 'Valid query';
GO
-- SET NOEXEC to ON.
SET NOEXEC ON;
GO
-- Inner join.
SELECT e.EmployeeID, e.Title, v.Name
FROM HumanResources.Employee e 
   INNER JOIN Purchasing.PurchaseOrderHeader poh
   ON e.EmployeeID = poh.EmployeeID
   INNER JOIN Purchasing.Vendor v
   ON poh.VendorID = v.VendorID;
GO
-- SET NOEXEC to OFF.
SET NOEXEC OFF;
GO

PRINT 'Invalid object name';
GO
-- SET NOEXEC to ON.
SET NOEXEC ON;
GO
-- Function name used is a reserved keyword.
USE AdventureWorks;
GO
CREATE FUNCTION dbo.Values(@EmployeeID INT)
RETURNS TABLE
AS
RETURN (SELECT PurchaseOrderID, TotalDue
   FROM dbo.PurchaseOrderHeader
   WHERE EmployeeID = @EmployeeID)
-- SET NOEXEC to OFF.
SET NOEXEC OFF
GO

PRINT 'Invalid syntax';
GO
-- SET NOEXEC to ON.
SET NOEXEC ON;
GO
-- Built-in function incorrectly invoked
SELECT *
FROM fn_helpcollations;
-- Reset SET NOEXEC to OFF.
SET NOEXEC OFF;
GO