write down,forget

SET PARSEONLY 与SET NOEXEC (Transact-SQL)

<Category: SQL Server> 查看评论

PARSEONLY
检查每个 Transact- 语句的语法并返回任何错误消息,但不编译和执行语句。
SET PARSEONLY { ON | OFF }

当 SET PARSEONLY 为 ON 时,SQL Server 只分析语句。当 SET PARSEONLY 为 OFF 时,SQL Server 编译并执行语句。

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

在存储过程或触发器中不要使用 PARSEONLY。如果 OFFSETS 选项为 ON 而且没有出现错误,则 SET PARSEONLY 返回偏移量。

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 设置是在执行或运行时设置,而不是在分析时设置。

 

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

本文来自: SET PARSEONLY 与SET NOEXEC (Transact-SQL)