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

关键字 : mssql-scripter,SQL Server

文章分类: 技术分享

创建时间: 2020年3月30日

                                              _.-"\
_.-" \
,-" \
\ \
\ \Zoomla逐浪CMS\
\ \ web开发秘笈\ \
\ \ z01.com _.-;
\ \ _.-" :
\ \,-" _.-"
\( _.-"
`--"

本文由国内专业从事高端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,命令行里执行下面命令:

pip install mssql-scripter

在Linux安装

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

pip –version

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

sudo apt-get install python-pip
sudo pip install --upgrade pip

c) 安装mssql-scripter:

sudo pip install mssql-scripter

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

Ubuntu 14 & 17

执行如下命令:

sudo apt-get update
sudo apt-get install libunwind8

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

文件‘/etc/apt/sources.list’需要更新:

deb http://ftp.us.debian.org/debian/ jessie main

执行如下命令:

sudo apt-get update
sudo apt-get install libunwind8

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

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

pip –version

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

sudo apt-get install python-pip
sudo pip install --upgrade pip

c) 安装mssql-scripter:

sudo pip install mssql-scripter

使用示例

经典的导出全库

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

显示进度导出脚本

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表,其它全生成

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表

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两张表

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三张表,包含删除旧表建新以及插入数据脚本

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

一些有用的参数

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

作为变量使用

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

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

# set environment variable MSSQL_SCRIPTER_CONNECTION_STRING with a connection string.
export MSSQL_SCRIPTER_CONNECTION_STRING='Server=myserver;Database=mydb;User Id=myuser;Password=mypassword;'
mssql-scripter # set environment variable MSSQL_SCRIPTER_PASSWORD so no password input is required.
export MSSQL_SCRIPTER_PASSWORD='ABC123'
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':

$ mssql-scripter -h
usage: mssql-scripter [-h] [--connection-string | -S ] [-d] [-U] [-P] [-f]
[--file-per-object] [--data-only | --schema-and-data]
[--script-create | --script-drop | --script-drop-create]
[--target-server-version {2005,2008,2008R2,2012,2014,2016,vNext,AzureDB,AzureDW}]
[--target-server-edition {Standard,Personal,Express,Enterprise,Stretch}]
[--include-objects [[...]]] [--exclude-objects [[...]]]
[--include-schemas [[...]]] [--exclude-schemas [[...]]]
[--include-types [[...]]] [--exclude-types [[...]]]
[--ansi-padding] [--append] [--check-for-existence] [-r]
[--convert-uddts] [--include-dependencies]
[--exclude-headers] [--constraint-names]
[--unsupported-statements]
[--disable-schema-qualification] [--bindings]
[--collation] [--exclude-defaults]
[--exclude-extended-properties] [--logins]
[--object-permissions] [--owner]
[--exclude-use-database] [--statistics]
[--change-tracking] [--exclude-check-constraints]
[--data-compressions] [--exclude-foreign-keys]
[--exclude-full-text-indexes] [--exclude-indexes]
[--exclude-primary-keys] [--exclude-triggers]
[--exclude-unique-keys] [--display-progress]
[--enable-toolsservice-logging] [--version] Microsoft SQL Server Scripter Command Line Tool. Version 1.0.0a14 optional arguments:
-h, --help show this help message and exit
--connection-string Connection string of database to script. If connection
string and server are not supplied, defaults to value
in environment variable
MSSQL_SCRIPTER_CONNECTION_STRING.
-S , --server Server name.
-d , --database Database name.
-U , --user Login ID for server.
-P , --password If not supplied, defaults to value in environment
variable MSSQL_SCRIPTER_PASSWORD.
-f , --file-path File to script out to or directory name if scripting
file per object.
--file-per-object By default script to a single file. If supplied and
given a directory for --file-path, script a file per
object to that directory.
--data-only By default only the schema is scripted. if supplied,
generate scripts that contains data only.
--schema-and-data By default only the schema is scripted. if supplied,
generate scripts that contain schema and data.
--script-create Script object CREATE statements.
--script-drop Script object DROP statements.
--script-drop-create Script object CREATE and DROP statements.
--target-server-version {2005,2008,2008R2,2012,2014,2016,vNext,AzureDB,AzureDW}
Script only features compatible with the specified SQL
Version.
--target-server-edition {Standard,Personal,Express,Enterprise,Stretch}
Script only features compatible with the specified SQL
Server database edition.
--include-objects [ [ ...]]
Database objects to include in script.
--exclude-objects [ [ ...]]
Database objects to exclude from script.
--include-schemas [ [ ...]]
Database objects of this schema to include in script.
--exclude-schemas [ [ ...]]
Database objects of this schema to exclude from
script.
--include-types [ [ ...]]
Database objects of this type to include in script.
--exclude-types [ [ ...]]
Database objects of this type to exclude from script.
--ansi-padding Generates ANSI Padding statements.
--append Append script to file.
--check-for-existence
Check that an object with the given name exists before
dropping or altering or that an object with the given
name does not exist before creating.
-r, --continue-on-error
Continue scripting on error.
--convert-uddts Convert user-defined data types to base types.
--include-dependencies
Generate script for the dependent objects for each
object scripted.
--exclude-headers Exclude descriptive headers for each object scripted.
--constraint-names Include system constraint names to enforce declarative
referential integrity.
--unsupported-statements
Include statements in the script that are not
supported on the target SQL Server Version.
--disable-schema-qualification
Do not prefix object names with the object schema.
--bindings Script options to set binding options.
--collation Script the objects that use collation.
--exclude-defaults Do not script the default values.
--exclude-extended-properties
Exclude extended properties for each object scripted.
--logins Script all logins available on the server, passwords
will not be scripted.
--object-permissions Generate object-level permissions.
--owner Script owner for the objects.
--exclude-use-database
Do not generate USE DATABASE statement.
--statistics Script all statistics.
--change-tracking Script the change tracking information.
--exclude-check-constraints
Exclude check constraints for each table or view
scripted.
--data-compressions Script the data compression information.
--exclude-foreign-keys
Exclude foreign keys for each table scripted.
--exclude-full-text-indexes
Exclude full-text indexes for each table or indexed
view scripted.
--exclude-indexes Exclude indexes (XML and clustered) for each table or
indexed view scripted.
--exclude-primary-keys
Exclude primary keys for each table or view scripted.
--exclude-triggers Exclude triggers for each table or view scripted.
--exclude-unique-keys
Exclude unique keys for each table or view scripted.
--display-progress Display scripting progress.
--enable-toolsservice-logging
Enable verbose logging.
--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

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

Dump database object data

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

Dump the database object schema and data

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

Include database objects

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

Exclude database objects

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

Target server version

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

Target server edition

# specify the edition of SQL Server the script will be run against
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.

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

Script data to a file

# script all the data to a file.
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:

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

原文:https://www.z01.com/help/DBA/3558.shtml (分享自Zoomla!逐浪CMS官网)

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

  1. 浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案

    作者 王枫发布于2014年2月19日 综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个 ...

  2. 转:浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案

    综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个时代决胜未来的关键因素,而基于大数据的 ...

  3. delphi 2010 导出sql server 数据到DBF乱码问题

    近日,由于业务需要导出sql server 数据到DBF文件,要查询多表记录,并适当处理后生成导出DBF文件,系统使用delphi2010平台开发. 首先按要求在VFP里创建DBF表,字段数有240个 ...

  4. 08Microsoft SQL Server 数据查询

    Microsoft SQL Server 数据查询 单表查询所有列 --查询所有行所有列 select all * from table; --查询不重复行的所有列 select distinct * ...

  5. SQL Server 复制 - 发布订阅(SQL Server 数据同步)

    原文:SQL Server 复制 - 发布订阅(SQL Server 数据同步) SQL Server的同步是通过SQL Server自带的复制工具来实现的,分发布和订阅2大步. A,复制-发布 发布 ...

  6. 浅谈SQL Server数据内部表现形式

    在上篇文章 浅谈SQL Server内部运行机制 中,与大家分享了SQL Server内部运行机制,通过上次的分享,相信大家已经能解决如下几个问题: 1.SQL Server 体系结构由哪几部分组成? ...

  7. SQL Server数据归档的解决方案

    SQL Server数据归档的解决方案   最近新接到的一项工作是把SQL Server中保存了四五年的陈年数据(合同,付款,报销等等单据)进行归档,原因是每天的数据增量很大,而历史数据又不经常使用, ...

  8. [转帖]微软 SQL Server 2008/R2 停止支持

    微软 SQL Server 2008/R2 停止支持 微软停止支持 SQLSERVER 2008R2 https://t.cj.sina.com.cn/articles/view/3172142827 ...

  9. Docker-compose搭建ELK环境并同步MS SQL Server数据

    前言 本文作为学习记录,供大家参考:一次使用阿里云(Aliyun)1核2G centos7.5 云主机搭建Docker下的ELK环境,并导入MS SQL Server的商品数据以供Kibana展示的配 ...

随机推荐

  1. Python3入门系列之-----环境搭建

    前  言 最近一直在学习Python,想用笔记的方式记录自己踩过的那些坑.俗话说:好记性不如烂笔头. 分享给想学Python的小伙伴.目前本人在学习Python+selenium.接口自动化,有兴趣的 ...

  2. P4544 [USACO10NOV]Buying Feed G

    part 1 暴力 不难发现有一个 $\mathcal O(K^2n)$ 的基础 dp: $$f_{i,j+l}=\min(f_{i,j+l},f_{i-1,j}+(x_i-x_{i-1})\time ...

  3. NOIP模拟79

    T1 F 解题思路 因为每个点会产生贡献当且仅当它在可以到他的点之前被删除,并且此题遵守期望的线性性. 因此设所有可以到达点 \(i\) 的数量为 \(c_i\) 那么答案就是 \(\sum \fra ...

  4. 【UE4 C++】调用外部链接库 lib静态库

    简述 本例以插件形式测试 使用Lib引用,打包程序运行不用再拷贝lib文件 需要 lib 文件和 .h 头文件 lib部分的代码 .h 头文件 #pragma once #ifndef __MYTES ...

  5. Java:LinkedHashMap类小记

    Java:LinkedHashMap类小记 对 Java 中的 LinkedHashMap类,做一个微不足道的小小小小记 概述 public class LinkedHashMap<K,V> ...

  6. 2021.9.21考试总结[NOIP模拟58]

    T1 lesson5! 开始以为是个无向图,直接不懂,跳去T2了. 之后有看了一眼发现可暴力,于是有了\(80pts\). 发现这个图是有拓扑序的,于是可以用拓扑排序找最长路径.先找原图内在最长路径上 ...

  7. 零基础玩转C语言单链表

    下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...

  8. Java并发:ReadWriteLock 读写锁

    读写锁在同一时刻可以允许多个线程访问,但是在写线程访问,所有的读线程和其他写线程均被阻塞. 读写锁不像 ReentrantLock 那些排它锁只允许在同一时刻只允许一个线程进行访问,读写锁可以允许多个 ...

  9. 第07课 OpenGL 光照和键盘(1)

    光照和键盘控制: 在这一课里,我们将添加光照和键盘控制,它让程序看起来更美观. 这一课我会教您如何使用三种不同的纹理滤波方式.教您如何使用键盘来移动场景中的对象,还会教您在OpenGL场景中应用简单的 ...

  10. 前端面试手写代码——call、apply、bind

    1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...