2009-11-04 54 views

回答

10

使用系統存儲過程sp_procoption定義希望在SQL Server服務啓動時執行的存儲過程。

exec sp_procoption 
     @ProcName = 'procedureName', 
     @OptionName = 'startup', 
     @OptionValue = 'true' 
+2

優秀的答案,謝謝:) –

+0

不客氣,很高興提供幫助。 –

1
USE master; 
GO 
-- first set the server to show advanced options 
EXEC sp_configure 'show advanced option', '1'; 
RECONFIGURE 
-- then set the scan for startup procs to 1 
EXEC sp_configure 'scan for startup procs', '1'; 
RECONFIGURE 

IF OBJECT_ID('spTest') IS NOT NULL 
    DROP PROC spTest 
GO 
-- crate a test stored procedure 
CREATE PROC spTest 
AS 
-- just create a sample database 
EXEC('CREATE database db1') 

GO 
-- set it to run at sql server start-up 
exec sp_procoption N'spTest', 'startup', 'on' 
+0

可能解釋上述代碼的註釋: 「只有系統管理員(sa)可以將存儲過程標記爲自動執行。此外,存儲過程必須位於master數據庫中,並由sa擁有,且不能有輸入或輸出參數「。 –

相關問題