SQL Server Management Studio 2012 - 将数据库的所有表导出为 csv
SQL Server Management Studio 2012 - Export all tables of database as csv
我在 SQL 服务器中有一个包含大量 table 的数据库,我希望以 csv 格式导出所有 table。来自之前提出的一个非常相似的问题 - Export from SQL Server 2012 to .CSV through Management Studio
Right click on your database in management studio and choose Tasks ->
Export Data...
Follow a wizard, and in destination part choose 'Flat File
Destination'. Type your file name and choose your options.
我想要的是一次导出所有 table 的能力。 SQL 服务器导入和导出向导一次只允许一个 table。如果您有一个非常大的数据库,这将非常麻烦。我认为更简单的解决方案可能涉及编写查询,但不确定。
不要单击 Export Data
,而是选择 Generate Scripts
。 Select你想要的表格,点击下一步然后点击Advanced
按钮。 General
下的最后一个选项是 Types of data to script
。选择 Schema and data
或 Data
.
导出向导一次只允许一个。我使用 powershell 脚本将我所有的表导出到 csv 中。如果对你有帮助,请试试。
$server = "SERVERNAME\INSTANCE"
$database = "DATABASE_NAME"
$tablequery = "SELECT schemas.name as schemaName, tables.name as tableName from sys.tables inner join sys.schemas ON tables.schema_id = schemas.schema_id"
#Delcare Connection Variables
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $tablequery
$command.Connection = $connection
#Load up the Tables in a dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
# Loop through all tables and export a CSV of the Table Data
foreach ($Row in $DataSet.Tables[0].Rows)
{
$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"
#Specify the output location of your dump file
$extractFile = "C:\mssql\export$($Row[0])_$($Row[1]).csv"
$command.CommandText = $queryData
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
}
谢谢
评论@annem-srinivas 解决方案:
如果您使用默认模式 (dbo) 以外的模式,请在他的脚本中更改以下内容:
$tablequery = "SELECT schemas.name as schemaName, tables.name AS tableName from sys.tables INNER JOIN sys.schemas ON schemas.schema_id
= tables.schema_id ORDER BY schemas.name, tables.name"
和
$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"
$extractFile = "C:\temp\export$($Row[0]).$($Row[1]).csv"
第一种方式
您可以将架构复制到临时数据库,然后使用该数据库导出所有 table(连同列名)。
步骤:
1) 首先,为所有 table 创建一个脚本:
任务->生成脚本->所有tables->单个sql文件
2) 创建一个虚拟数据库和 运行 这个脚本。
3) 右键单击虚拟数据库和 select 任务->导出数据-> select 源数据->select 目标为 Microsoft Excel 并提供它的路径->执行
您将获得一个包含所有 table 及其列的电子表格。
第二种方式
执行以下查询,它将在第一列中给出所有 table 个名称,并在结果的第二列中给出相应的列名。
select t.name ,c.name from sys.columns c
inner join sys.tables t
on t.object_id = c.object_id
order by t.name
复制此结果并将其粘贴到 CSV 文件中。
sree 的回答很好。对于我的数据库,因为有多个模式,我改变了这个:
$tablequery = "SELECT schemas.name as schemaName, tables.name as tableName from sys.tables inner join sys.schemas ON tables.schema_id = schemas.schema_id"
然后还有
$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"
#Specify the output location of your dump file
$extractFile = "C:\mssql\export$($Row[0])_$($Row[1]).csv"
我在 SQL 服务器中有一个包含大量 table 的数据库,我希望以 csv 格式导出所有 table。来自之前提出的一个非常相似的问题 - Export from SQL Server 2012 to .CSV through Management Studio
Right click on your database in management studio and choose Tasks -> Export Data...
Follow a wizard, and in destination part choose 'Flat File Destination'. Type your file name and choose your options.
我想要的是一次导出所有 table 的能力。 SQL 服务器导入和导出向导一次只允许一个 table。如果您有一个非常大的数据库,这将非常麻烦。我认为更简单的解决方案可能涉及编写查询,但不确定。
不要单击 Export Data
,而是选择 Generate Scripts
。 Select你想要的表格,点击下一步然后点击Advanced
按钮。 General
下的最后一个选项是 Types of data to script
。选择 Schema and data
或 Data
.
导出向导一次只允许一个。我使用 powershell 脚本将我所有的表导出到 csv 中。如果对你有帮助,请试试。
$server = "SERVERNAME\INSTANCE"
$database = "DATABASE_NAME"
$tablequery = "SELECT schemas.name as schemaName, tables.name as tableName from sys.tables inner join sys.schemas ON tables.schema_id = schemas.schema_id"
#Delcare Connection Variables
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $tablequery
$command.Connection = $connection
#Load up the Tables in a dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
# Loop through all tables and export a CSV of the Table Data
foreach ($Row in $DataSet.Tables[0].Rows)
{
$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"
#Specify the output location of your dump file
$extractFile = "C:\mssql\export$($Row[0])_$($Row[1]).csv"
$command.CommandText = $queryData
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
}
谢谢
评论@annem-srinivas 解决方案: 如果您使用默认模式 (dbo) 以外的模式,请在他的脚本中更改以下内容:
$tablequery = "SELECT schemas.name as schemaName, tables.name AS tableName from sys.tables INNER JOIN sys.schemas ON schemas.schema_id
= tables.schema_id ORDER BY schemas.name, tables.name"
和
$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"
$extractFile = "C:\temp\export$($Row[0]).$($Row[1]).csv"
第一种方式
您可以将架构复制到临时数据库,然后使用该数据库导出所有 table(连同列名)。
步骤:
1) 首先,为所有 table 创建一个脚本:
任务->生成脚本->所有tables->单个sql文件
2) 创建一个虚拟数据库和 运行 这个脚本。
3) 右键单击虚拟数据库和 select 任务->导出数据-> select 源数据->select 目标为 Microsoft Excel 并提供它的路径->执行
您将获得一个包含所有 table 及其列的电子表格。
第二种方式
执行以下查询,它将在第一列中给出所有 table 个名称,并在结果的第二列中给出相应的列名。
select t.name ,c.name from sys.columns c
inner join sys.tables t
on t.object_id = c.object_id
order by t.name
复制此结果并将其粘贴到 CSV 文件中。
sree 的回答很好。对于我的数据库,因为有多个模式,我改变了这个:
$tablequery = "SELECT schemas.name as schemaName, tables.name as tableName from sys.tables inner join sys.schemas ON tables.schema_id = schemas.schema_id"
然后还有
$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"
#Specify the output location of your dump file
$extractFile = "C:\mssql\export$($Row[0])_$($Row[1]).csv"