【问题标题】:Script that will make a SQL Server script from a database将从数据库生成 SQL Server 脚本的脚本
【发布时间】:2020-04-09 11:19:00
【问题描述】:

我有一个 SQL Server 数据库,其中包含我想定期编写脚本的表和其他对象。我的想法是我将创建一个自动构建,它将创建一个容器,该容器是该数据库的缩小版本(用于测试)。

我可以轻松地手动编写数据库脚本,但是我必须使生成的脚本保持最新。

我想知道是否有办法以编程方式连接到正在运行的 SQL Server 实例并(根据一些输入)生成特定表、存储过程、用户定义类型和数据的脚本?

【问题讨论】:

  • 自动构建 SQL Server 数据库?使用 VS SQL 数据库项目并通过 .dacpac 文件部署可以很好地涵盖这一点
  • 也许读过这个sqlshack.com/…
  • 使用Sql Server,最好使用.bak文件。您可以在另一台机器上从中恢复。

标签: sql-server automation scripting containers


【解决方案1】:

如果您使用 .NET 其他可以调用 .NET 对象的其他语言进行编码,则可以只使用 SMO(SQL 管理对象)中的脚本类。

这里的 SQL Server 文档中有工作示例:

https://docs.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/tasks/scripting?view=sql-server-ver15

它们旨在完全按照您的要求进行。

【讨论】:

    【解决方案2】:

    嗯,我认为备份是最简单的方法。

    或者,设置多个作业,将所有内容包装在一个 SProc 中,并安排它以您希望的任何频率运行。

    To create and attach a schedule to a job
    
        In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
    
        Expand SQL Server Agent, expand Jobs, right-click the job you want to schedule, and click Properties.
    
        Select the Schedules page, and then click New.
    
        In the Name box, type a name for the new schedule.
    
        Clear the Enabled check box if you do not want the schedule to take effect immediately following its creation.
    
        For Schedule Type, select one of the following:
    
            Click Start automatically when SQL Server Agent starts to start the job when the SQL Server Agent service is started.
    
            Click Start whenever the CPUs become idle to start the job when the CPUs reach an idle condition.
    
            Click Recurring if you want a schedule to run repeatedly. To set the recurring schedule, complete the Frequency, Daily Frequency, and Duration groups on the dialog.
    
            Click One time if you want the schedule to run only once. To set the One time schedule, complete the One-time occurrence group on the dialog.
    

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多