【问题标题】:How to run a stored procedure when SQL Server Express Edition starts?SQL Server Express Edition 启动时如何运行存储过程?
【发布时间】:2010-12-13 02:49:15
【问题描述】:

SQL Server Express Edition 启动时如何运行存储过程?

【问题讨论】:

    标签: sql sql-server sql-server-express scheduled-tasks


    【解决方案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'
    

    【讨论】:

    • 一个注释可以解释上面的代码:“只有系统管理员(sa)才能将存储过程标记为自动执行。另外,存储过程必须在master数据库中并且由sa拥有并且不能有输入或输出参数。”
    【解决方案2】:

    使用系统存储过程sp_procoption 定义您希望在 SQL Server 服务启动时执行的存储过程。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多