不允許來自觸發程序的結果 (伺服器組態選項)

適用於:SQL Server

使用 disallow results from triggers 選項來控制觸發程序是否要傳回結果集。 如果觸發程序會傳回結果集,其可能會導致非專用應用程式發生非預期的行為。

重要

從觸發程序傳回結果集的能力將會在未來版本的 SQL Server 中移除。 請在新的開發工作中避免從觸發程序傳回結果集,並計畫修改目前如此運作的應用程式。 若要避免觸發程序傳回結果集,請將不允許來自觸發程序的結果選項更改為 1。 不允許觸發程式結果選項的預設設定將會在未來的 SQL Server 版本中設定為 1

若設為 1 disallow results from triggers 選項就會設為 ON。 這個選項的預設值是 0 (OFF)。 若此選項設為 1 (ON),則觸發程序嘗試傳回結果集的任何動作都會失敗,而使用者會收到下列錯誤訊息:

Msg 524, Level 16, State 1, Procedure <Procedure Name>, Line <Line#>

A trigger returned a resultset and the server option 'disallow_results_from_triggers' is true.

[不允許來自觸發程序的結果] 選項會套用至 Microsoft SQL Server 執行個體層級,而且會決定執行個體內所有現有觸發程序的行為。

disallow results from triggers 選項是進階選項。 若使用 sp_configure 系統預存程序來變更設定,只有當 [顯示進階選項] 設為 1 時,才能變更觸發程序不允許的結果。 設定會立即生效,伺服器不必重新啟動。

您可以使用下列 Transact-SQL 程式代碼來檢查選項是否已設定正確:

-- Check the current value for the option
SELECT [name], value_in_use
FROM sys.configurations
WHERE [name] LIKE 'disallow results from triggers';

-- Set the disallow results from triggers option to 1. This is an advanced option so that must be enabled first
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
GO

-- Set the disallow results from triggers option
EXEC sp_configure 'disallow results from triggers', 1;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE
GO

-- Validate that the option is set to 1
SELECT [name], value_in_use
FROM sys.configurations
WHERE [name] LIKE 'disallow results from triggers';
GO

另請參閱