一行导出所有任意微软SQL server数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解

Posted zoomlacms

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一行导出所有任意微软SQL server数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解相关的知识,希望对你有一定的参考价值。

文章标题: 一行导出所有任意微软SQL server数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解
关键字 : mssql-scripter,SQL Server
文章分类: 技术分享

创建时间: 2020年3月30日

  1. _.-"
  2. _.-"
  3. ,-"
  4. oomla逐浪CMS
  5. web开发秘笈
  6. z01.com _.-;
  7. _.-" :
  8. ,-" _.-"
  9. ( _.-"
  10. `--"

 

本文由国内专业从事高端web与全栈开发、国内首家提供全领域生态环境(呈现、内核、前端)的Zoomla!逐浪CMS团队首发,官网www.z01.com

什么是mssql-scripter

技术图片

一款由微软官方发布的数据库管理工具,可以通过命令行,直接生成SQL脚本。

在实际开发中,如果想提高数据库生成效率,则用它能事半功倍。

如果你语言足够熟悉,当然也可以用sqlcms、dotNETcore、Powershell工具在微软平台,快速的对数据库进行各类建模,而这款工具则提供了另一种跨平台的可能。

让我们看下官方的介绍:

我们很高兴推出mssql-scripter,这是一种用于编写SQL Server数据库脚本的多平台命令行体验。

mssql-scripter是等同于SSMS中广泛使用的“生成脚本向导”经验的多平台命令行。您可以在Linux,macOS和Windows上使用mssql-scripter为在任何地方运行的SQL Server,Azure SQL数据库和Azure SQL数据仓库中的数据库对象生成数据定义语言(DDL)和数据操作语言(DML)T-SQL脚本。您可以将生成的T-SQL脚本保存到.sql文件,或将其通过管道传输到标准nix实用程序(例如sed,awk,grep)以进行进一步的转换。您可以编辑生成的脚本或将其检入源代码管理,然后使用标准的多平台SQL命令行工具(如sqlcmd)在现有SQL数据库部署过程和DevOps管道中执行脚本。

mssql-scripter使用Python构建,并结合了新的Azure CLI 2.0工具的可用性原则。

官方Github库:https://github.com/microsoft/mssql-scripter

截止本文写作时,版本号:1.0.0a23

有何特色

  • 它是跨平台的
  • 它是基于python的(虽然python最近炒得很热,其实微软的powershell和.netcore也有很多超凡之处,只是微软为python提供了一种操作可能,展现了巨硬团队在跨平台方面的实力。
  • 它更精简
  • 它能集成在环境变量。

应用场景

除了普通开发的、部署外,还可以用于CI/CD持续部署,亦即自动化部署。

如何安装

首先是安装python

安装Python,最新安装包下载地址:https://www.python.org/downloads/,注意安装的时候要选择”Add Python to PATH”选项:

技术图片

安装有一些选项,用默认就可以了,以下为相关选项说明:

  • nstall for all users 所有用户可使用
  • Associate files with Python 关联PY相关的文件
  • Create shortcuts for installed applications 创建桌面的快捷方式
  • Add Python to environment variables 添加系统变量(windows系统)
  • Precompile standard library 安装预编译标准库
  • Download debugging symbols 安装调试模块(开发者可选择,运用于开发环境)
  • Download debug binaries安装用于VS的调试符号(二进制),如果不使用VS作为开发工具,则不用勾选(支持VS2015以上),适用于.NET开发。

技术图片

b) 安装mssql-scripter,命令行里执行下面命令:

  1. pip install mssql-scripter

在Linux安装

a) 检查pip版本,是否是9.0及其以上:

  1. pip version

b) 如果pip未安装或者版本低于9.0,使用如下命令安装以及升级版本:

  1. sudo apt-get install python-pip
  2. sudo pip install --upgrade pip

c) 安装mssql-scripter:

  1. sudo pip install mssql-scripter

如果系统是Ubuntu或者Debian,需要安装libunwind8软件包:

Ubuntu 14 & 17
执行如下命令:

  1. sudo apt-get update
  2. sudo apt-get install libunwind8

Debian 8(无实测环境,仅官方文档)
文件‘/etc/apt/sources.list’需要更新:

deb http://ftp.us.debian.org/debian/ jessie main
执行如下命令:

  1. sudo apt-get update
  2. sudo apt-get install libunwind8

macOS(无实测环境,仅官方文档)

a) 检查pip版本,是否是9.0及其以上:

  1. pip version

b) 如果pip未安装或者版本低于9.0,使用如下命令安装以及升级版本:

  1. sudo apt-get install python-pip
  2. sudo pip install --upgrade pip

c) 安装mssql-scripter:

  1. sudo pip install mssql-scripter

使用示例

经典的导出全库

  1. # 微软官方示例
  2. mssql-scripter -S localhost -d AdventureWorks -U sa --schema-and-data > ./adventureworks.sql
  3. # 实际用例
  4. mssql-scripter -S 192.168.1.4 -d ZoomlaCMS -U ZoomlaDB_user -P 数据库密码 --schema-and-data --display-progress > ./adventureworks3.sql

显示进度导出脚本

  1. mssql-scripter -S 192.168.1.4 -d ZoomlaCMS -U ZoomlaDB_user -P 数据库密码 --schema-and-data --exclude-use-database --display-progress > ./adventureworks3.sql

显示进度不带use数据库,排除ZL_Node表,其它全生成

  1. mssql-scripter -S 192.168.1.4 -d ZoomlaCMS -U ZoomlaDB_user -P 数据库密码 --schema-and-data --exclude-objects ZL_Node --exclude-use-database --display-progress > ./adventureworks4.sql

显示进度不带use数据库,仅生成ZL_Node表

  1. mssql-scripter -S 192.168.1.4 -d ZoomlaCMS -U ZoomlaDB_user -P 数据库密码 --schema-and-data --include-objects ZL_Node --exclude-use-database --display-progress > ./adventureworksA.sql

显示进度不带use数据库,生成ZL_Node,ZL_User两张表

  1. mssql-scripter -S 192.168.1.4 -d ZoomlaCMS -U ZoomlaDB_user -P 数据库密码 --schema-and-data --include-objects ZL_Node ZL_User --exclude-use-database --display-progress > ./adventureworksB.sql

显示进度不带use数据库,生成ZL_Node,ZL_User,ZL_CommonModel三张表,包含删除旧表建新以及插入数据脚本

  1. mssql-scripter -S 192.168.1.4 -d ZoomlaCMS -U ZoomlaDB_user -P 数据库密码 --schema-and-data --include-objects ZL_Node ZL_User ZL_CommonModel --exclude-use-database --script-drop-create --display-progress > ./adventureworksB.sql

一些有用的参数

  1. --file-per-object默认情况下,脚本是单个文件。如果提供并且给--file-path目录,每个脚本该目录的对象。
  2. --data-only默认情况下,仅对架构进行脚本编写。如果提供,生成仅包含数据的脚本。
  3. --schema-and-data默认情况下,仅对模式进行脚本编写。如果提供,生成包含架构和数据的脚本。
  4. --script-create脚本对象CREATE语句。
  5. --script-drop脚本对象DROP语句。
  6. --script-drop-create脚本对象CREATEDROP语句。
  7. -exclude-use-database
  8. 不生成USE DATABASE语句。
  9. --data-compressions编写数据压缩信息脚本。
  10. --display-progress显示脚本编制进度

作为变量使用

喜欢极简的你,一定嫌写数据库名和密码这些太复杂,没问题,微软爸爸想到了,你还可以将这些作为变量写到系统中。

可以把连接字符串设置成环境变量:

  1. # set environment variable MSSQL_SCRIPTER_CONNECTION_STRING with a connection string.
  2. export MSSQL_SCRIPTER_CONNECTION_STRING=‘Server=myserver;Database=mydb;User Id=myuser;Password=mypassword;‘
  3. mssql-scripter
  4. # set environment variable MSSQL_SCRIPTER_PASSWORD so no password input is required.
  5. export MSSQL_SCRIPTER_PASSWORD=‘ABC123‘
  6. mssql-scripter -S localhost -d AdventureWorks -U sa

微软团队官方使用说明(英文)

Usage Guide

Contents:

Options

Examples

Environment Variables

Description

mssql-scripter is the multiplatform command line equivalent of the widely used Generate Scripts Wizard experience in SSMS.

You can use mssql-scripter on Linux, macOS, and Windows to generate data definition language (DDL) and data manipulation language (DML) T-SQL scripts for database objects in SQL Server running anywhere, Azure SQL Database, and Azure SQL Data Warehouse. You can save the generated T-SQL script to a .sql file or pipe it to standard *nix utilities (for example, sed, awk, grep) for further transformations. You can edit the generated script or check it into source control and subsequently execute the script in your existing SQL database deployment processes and DevOps pipelines with standard multiplatform SQL command line tools such as sqlcmd.

Options

For option parameters, pass in ‘-h’:

  1. $ mssql-scripter -h
  2. usage: mssql-scripter [-h] [--connection-string | -S ] [-d] [-U] [-P] [-f]
  3. [--file-per-object] [--data-only | --schema-and-data]
  4. [--script-create | --script-drop | --script-drop-create]
  5. [--target-server-version {2005,2008,2008R2,2012,2014,2016,vNext,AzureDB,AzureDW}]
  6. [--target-server-edition {Standard,Personal,Express,Enterprise,Stretch}]
  7. [--include-objects [[...]]] [--exclude-objects [[...]]]
  8. [--include-schemas [[...]]] [--exclude-schemas [[...]]]
  9. [--include-types [[...]]] [--exclude-types [[...]]]
  10. [--ansi-padding] [--append] [--check-for-existence] [-r]
  11. [--convert-uddts] [--include-dependencies]
  12. [--exclude-headers] [--constraint-names]
  13. [--unsupported-statements]
  14. [--disable-schema-qualification] [--bindings]
  15. [--collation] [--exclude-defaults]
  16. [--exclude-extended-properties] [--logins]
  17. [--object-permissions] [--owner]
  18. [--exclude-use-database] [--statistics]
  19. [--change-tracking] [--exclude-check-constraints]
  20. [--data-compressions] [--exclude-foreign-keys]
  21. [--exclude-full-text-indexes] [--exclude-indexes]
  22. [--exclude-primary-keys] [--exclude-triggers]
  23. [--exclude-unique-keys] [--display-progress]
  24. [--enable-toolsservice-logging] [--version]
  25. Microsoft SQL Server Scripter Command Line Tool. Version 1.0.0a14
  26. optional arguments:
  27. -h, --help show this help message and exit
  28. --connection-string Connection string of database to script. If connection
  29. string and server are not supplied, defaults to value
  30. in environment variable
  31. MSSQL_SCRIPTER_CONNECTION_STRING.
  32. -S , --server Server name.
  33. -d , --database Database name.
  34. -U , --user Login ID for server.
  35. -P , --password If not supplied, defaults to value in environment
  36. variable MSSQL_SCRIPTER_PASSWORD.
  37. -f , --file-path File to script out to or directory name if scripting
  38. file per object.
  39. --file-per-object By default script to a single file. If supplied and
  40. given a directory for --file-path, script a file per
  41. object to that directory.
  42. --data-only By default only the schema is scripted. if supplied,
  43. generate scripts that contains data only.
  44. --schema-and-data By default only the schema is scripted. if supplied,
  45. generate scripts that contain schema and data.
  46. --script-create Script object CREATE statements.
  47. --script-drop Script object DROP statements.
  48. --script-drop-create Script object CREATE and DROP statements.
  49. --target-server-version {2005,2008,2008R2,2012,2014,2016,vNext,AzureDB,AzureDW}
  50. Script only features compatible with the specified SQL
  51. Version.
  52. --target-server-edition {Standard,Personal,Express,Enterprise,Stretch}
  53. Script only features compatible with the specified SQL
  54. Server database edition.
  55. --include-objects [ [ ...]]
  56. Database objects to include in script.
  57. --exclude-objects [ [ ...]]
  58. Database objects to exclude from script.
  59. --include-schemas [ [ ...]]
  60. Database objects of this schema to include in script.
  61. --exclude-schemas [ [ ...]]
  62. Database objects of this schema to exclude from
  63. script.
  64. --include-types [ [ ...]]
  65. Database objects of this type to include in script.
  66. --exclude-types [ [ ...]]
  67. Database objects of this type to exclude from script.
  68. --ansi-padding Generates ANSI Padding statements.
  69. --append Append script to file.
  70. --check-for-existence
  71. Check that an object with the given name exists before
  72. dropping or altering or that an object with the given
  73. name does not exist before creating.
  74. -r, --continue-on-error
  75. Continue scripting on error.
  76. --convert-uddts Convert user-defined data types to base types.
  77. --include-dependencies
  78. Generate script for the dependent objects for each
  79. object scripted.
  80. --exclude-headers Exclude descriptive headers for each object scripted.
  81. --constraint-names Include system constraint names to enforce declarative
  82. referential integrity.
  83. --unsupported-statements
  84. Include statements in the script that are not
  85. supported on the target SQL Server Version.
  86. --disable-schema-qualification
  87. Do not prefix object names with the object schema.
  88. --bindings Script options to set binding options.
  89. --collation Script the objects that use collation.
  90. --exclude-defaults Do not script the default values.
  91. --exclude-extended-properties
  92. Exclude extended properties for each object scripted.
  93. --logins Script all logins available on the server, passwords
  94. will not be scripted.
  95. --object-permissions Generate object-level permissions.
  96. --owner Script owner for the objects.
  97. --exclude-use-database
  98. Do not generate USE DATABASE statement.
  99. --statistics Script all statistics.
  100. --change-tracking Script the change tracking information.
  101. --exclude-check-constraints
  102. Exclude check constraints for each table or view
  103. scripted.
  104. --data-compressions Script the data compression information.
  105. --exclude-foreign-keys
  106. Exclude foreign keys for each table scripted.
  107. --exclude-full-text-indexes
  108. Exclude full-text indexes for each table or indexed
  109. view scripted.
  110. --exclude-indexes Exclude indexes (XML and clustered) for each table or
  111. indexed view scripted.
  112. --exclude-primary-keys
  113. Exclude primary keys for each table or view scripted.
  114. --exclude-triggers Exclude triggers for each table or view scripted.
  115. --exclude-unique-keys
  116. Exclude unique keys for each table or view scripted.
  117. --display-progress Display scripting progress.
  118. --enable-toolsservice-logging
  119. Enable verbose logging.
  120. --version show program‘s version number and exit

Examples

Below are example commands that run against the AdventureWorks database. Here is the list of examples:

Dump datbase object schema

Dump datbase object data

Dump database object schema and data

Include database objects

Exclude database objects

Target server version

Target server edition

Pipe a generated script to sed

Script data to a file

Dump database object schema

  1. # generate DDL scripts for all objects in the Adventureworks database and save the script to a file
  2. mssql-scripter -S localhost -d AdventureWorks -U sa
  3. # alternatively, specify the schema only flag to generate DDL scripts for all objects in the Adventureworks database and save the script to a file
  4. mssql-scripter -S localhost -d AdventureWorks -U sa -f ./adventureworks.sql

Dump database object data

  1. # generate DDL scripts for all objects in the Adventureworks database and save the script to stdout.
  2. mssql-scripter -S localhost -d AdventureWorks -U sa --data-only

Dump the database object schema and data

  1. # script the database schema and data piped to a file.
  2. mssql-scripter -S localhost -d AdventureWorks -U sa --schema-and-data > ./adventureworks.sql
  3. # execute the generated above script with sqlcmd
  4. sqlcmd -S mytestserver -U sa -i ./adventureworks.sql

Include database objects

  1. # generate DDL scripts for objects that contain ‘Employee‘ in their name to stdout
  2. mssql-scripter -S localhost -d AdventureWorks -U sa --include-objects Employee
  3. # generate DDL scripts for the dbo schema and pipe the output to a file
  4. mssql-scripter -S localhost -d AdventureWorks -U sa --include-objects dbo. > ./dboschema.sql

Exclude database objects

  1. # generate DDL scripts for objects that do not contain ‘Sale‘ in their name to stdout
  2. mssql-scripter -S localhost -d AdventureWorks -U sa --exclude-objects Sale

Target server version

  1. # specify the version of SQL Server the script will be run against
  2. mssql-scripter -S myServer -d AdventureWorks -U myUser –-target-server-version "AzureDB" > myData.sql

Target server edition

  1. # specify the edition of SQL Server the script will be run against
  2. mssql-scripter -S localhost -d AdventureWorks -U myUser –-target-server-edition "Enterprise" > myData.sql

Pipe a generated script to sed

Note this example is for Linux and macOS usage.

  1. # change a schema name in the generated DDL script
  2. # 1) generate DDL scripts for all objects in the Adventureworks database
  3. # 2) pipe generated script to sed and change all occurrences of SalesLT to SalesLT_test and save the script to a file
  4. $ mssql-scripter -S localhost -d Adventureworks -U sa | sed -e "s/SalesLT./SalesLT_test./g" > adventureworks_SalesLT_test.sql

Script data to a file

  1. # script all the data to a file.
  2. mssql-scripter -S localhost -d AdventureWorks -U sa --data-only > ./adventureworks-data.sql

Environment Variables

You can set environment variables for your connection string through the following steps:

  1. # set environment variable MSSQL_SCRIPTER_CONNECTION_STRING with a connection string.
  2. $ export MSSQL_SCRIPTER_CONNECTION_STRING=‘Server=myserver;Database=mydb;User Id=myuser;Password=mypassword;‘
  3. $ mssql-scripter
  4. # set environment variable MSSQL_SCRIPTER_PASSWORD so no password input is required.
  5. $ export MSSQL_SCRIPTER_PASSWORD=‘ABC123‘
  6. $ mssql-scripter -S localhost -d AdventureWorks -U sa

以上是关于一行导出所有任意微软SQL server数据脚本-基于Python的微软官方mssql-scripter工具使用全讲解的主要内容,如果未能解决你的问题,请参考以下文章

怎么导出Sql Server数据库表结构和数据的脚本

寻找为整个 SQL Server 数据库创建导出脚本的工具 [关闭]

怎么导出Sql Server数据库表结构和数据的脚本

MS SQL SERVER执行大脚本文件时,提示“内存不足”的解决办法

怎么导出Sql Server数据库表结构和数据的脚本

怎么导出Sql Server数据库表结构和数据的脚本