SET NOEXEC (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 AdventureWorks2012;
GO
PRINT 'Valid query';
GO
-- SET NOEXEC to ON.
SET NOEXEC ON;
GO
-- Inner join.
SELECT e.BusinessEntityID, e.JobTitle, v.Name
FROM HumanResources.Employee AS e
INNER JOIN Purchasing.PurchaseOrderHeader AS poh
ON e.BusinessEntityID = poh.EmployeeID
INNER JOIN Purchasing.Vendor AS v
ON poh.VendorID = v.BusinessEntityID;
GO
-- SET NOEXEC to OFF.
SET NOEXEC OFF;
GO
PRINT 'Invalid object name';
GO
-- SET NOEXEC to ON.
SET NOEXEC ON;
GO
-- Function name uses is a reserved keyword.
USE AdventureWorks2012;
GO
CREATE FUNCTION dbo.Values(@BusinessEntityID int)
RETURNS TABLE
AS
RETURN (SELECT PurchaseOrderID, TotalDue
FROM dbo.PurchaseOrderHeader
WHERE VendorID = @BusinessEntityID);
-- 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