【问题标题】:Generate a SQL create table script using powershell使用 powershell 生成 SQL 创建表脚本
【发布时间】:2020-12-02 10:35:15
【问题描述】:

我希望使用 powershell 能够快速生成带有创建表语句的 SQL 脚本,该语句能够重新创建现有数据库中的所有表。唯一的问题是我想调整一些选项,例如关闭身份。

我就是不知道该怎么做!

我已经设置了一个 $server 变量,并设置了一个名为 $tables 的变量来获取该特定数据库中的所有表。 然后我使用一个循环: foreach ($tables in $tables) {$table.Script() }

这可行,但我只是不知道如何添加脚本选项,例如 NoIdentities = $True

谁能帮帮我?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我曾经不得不做很多重复的打字工作,包括为 数据库中的每个表。所以我开发了一个通用工具,它适合这种类型的工作。我按原样包括该工具,以及该工具的示例运行,旨在 为每个表和每个数据库用户类别生成一系列授权命令。

    我的工具使用 CSV 文件而不是直接使用数据库。我发现为许多不同的任务生成 CSV 文件和模板相当容易。你的旅费可能会改变。也许您可以从这个工具入手并根据您的需要进行调整。

    这是工具,以及运行示例。

    <#
    .SYNOPSIS
        Generates multiple expansions of a template,
        driven by data in a CSV file.
    .DESCRIPTION
        This function is a table driven template tool. 
    
        It generates output from a template and
        a driver table.  The template file contains plain
        text and embedded variables.  The driver table 
        (in a csv file) has one column for each variable, 
        and one row for each expansion to be generated.
    #>
    function Expand-csv {
    
    
       [CmdletBinding()]
       Param(
          [Parameter(Mandatory=$true)]
          [string] $driver,
          [Parameter(Mandatory=$true)]
          [string] $template
       )
       Process
       {
          $xp = (Get-Content $template) -join "`r`n"
    
          Import-Csv $driver | % {
             $_.psobject.properties | % {Set-variable -name $_.name -value $_.value}
             $ExecutionContext.InvokeCommand.ExpandString($xp) 
             }
       }
    }
    
    # Now do a sample run of Expand-csv
    # then display inputs and output
    
    Expand-csv grants.csv grants.tmplt > grants.sql
    get-content grants.tmplt
    import-csv grants.csv
    get-content grants.sql
    

    这是上面运行的结果:

    PS> C:\Users\David\Software\Powershell\test\sample.ps1
    
    
    grant $privs 
       on $table 
       to $user;
    
    
    
    privs       table       user                
    -----       -----       ----                
    ALL         Employees   DBA                 
    READ        Employees   Analyst             
    READ, WRITE Employees   Application         
    ALL         Departments DBA                 
    READ        Departments Analyst, Application
    
    
    grant ALL 
       on Employees 
       to DBA;
    
    
    
    
    grant READ 
       on Employees 
       to Analyst;
    
    
    
    
    grant READ, WRITE 
       on Employees 
       to Application;
    
    
    
    
    grant ALL 
       on Departments 
       to DBA;
    
    
    
    
    grant READ 
       on Departments 
       to Analyst, Application;
    
    
    
    
    
    PS> 
    

    在现实生活中,我在我的 $profile 文件中定义了该工具,因此只要我在 powershell 中就可以使用它。

    我不确定这是否适用于您的情况。这并不能解决您描述的确切情况,但您可以调整该技术。

    【讨论】:

      【解决方案2】:

      如果您仍在寻找解决方案,我可以建议我在单个表格中使用的这个小脚本。您应该能够相当轻松地更新它以支持多个表。注意行“$scripter.Options.NoIdentities = $true;”下面。

      param
      (
        [string] $server,
        [string] $database,
        [string] $schema,
        [string] $table
      )
      
      [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
      
      $srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $server
      $db = New-Object ("Microsoft.SqlServer.Management.SMO.Database")
      $tbl = New-Object ("Microsoft.SqlServer.Management.SMO.Table")
      $scripter = New-Object ("Microsoft.SqlServer.Management.SMO.Scripter") ($server)
      $db = $srv.Databases[$database]
      $tbl = $db.tables | Where-object {$_.schema -eq $schema-and$_.name -eq $table}
      
      $scripter.Options.ScriptSchema = $true;
      $scripter.Options.ScriptData = $false;
      
      $scripter.Options.NoCommandTerminator = $false;
      $scripter.Options.NoCollation = $true;
      $scripter.Options.NoIdentities = $true;
      
      $scripter.EnumScript($tbl)
      

      将其保存到“table.ps1”并通过传递参数值在 PowerShell 中执行:

      & table.ps1 -server SERVER\INSTANCE -database MyDB -schema dbo -table MyTable
      

      让我知道它是否有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-18
        • 2011-11-24
        • 1970-01-01
        • 2019-10-29
        • 1970-01-01
        • 2021-05-02
        • 2022-01-11
        相关资源
        最近更新 更多