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 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