简体   繁体   English

SQL Server Management Studio 2012 - 将数据库的所有表导出为 csv

[英]SQL Server Management Studio 2012 - Export all tables of database as csv

I have a database in SQL Server with a lot of tables and wish to export all tables in csv format.我在 SQL 服务器中有一个数据库,里面有很多表,希望以 csv 格式导出所有表。 From a very similar question asked previously - Export from SQL Server 2012 to.CSV through Management Studio来自之前提出的一个非常相似的问题 - Export from SQL Server 2012 to.CSV through Management Studio

Right click on your database in management studio and choose Tasks -> Export Data...在 Management Studio 中右键单击您的数据库,然后选择任务 -> 导出数据...

Follow a wizard, and in destination part choose 'Flat File Destination'.按照向导操作,在目标部分选择“平面文件目标”。 Type your file name and choose your options.键入您的文件名并选择您的选项。

What I want is the capability to export all tables at once.我想要的是一次导出所有表的能力。 The SQL Server Import and Export Wizard only permits one table at a time. SQL 服务器导入和导出向导一次只允许一个表。 This is pretty cumbersome, if you have a very big database.如果您有一个非常大的数据库,这将非常麻烦。 I think a simpler solution might involve writing a query, but not sure.我认为更简单的解决方案可能涉及编写查询,但不确定。

The export wizard allows only one at a time.导出向导一次只允许一个。 I used the powershell script to export all my tables into csv.我使用 powershell 脚本将所有表导出到 csv 中。 Please try this if it helps you.如果对你有帮助,请试试这个。

$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
}

Thanks谢谢

Instead of clicking Export Data , choose Generate Scripts .不要单击“ Export Data ,而是选择“ Generate Scripts Select the tables you want, click next and click the Advanced button.选择您想要的表,单击下一步并单击Advanced按钮。 The last option under General is Types of data to script . General下的最后一个选项是Types of data to script Chose Schema and data or just Data .选择Schema and data或只是Data

The answer by sree is great. sree 的回答很棒。 For my db, because there are multiple schemas, I changed this:对于我的数据库,因为有多个模式,我改变了这个:

$tablequery = "SELECT schemas.name as schemaName, tables.name as tableName from sys.tables inner join sys.schemas ON tables.schema_id = schemas.schema_id"

and then also然后也

$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"

#Specify the output location of your dump file
$extractFile = "C:\mssql\export\$($Row[0])_$($Row[1]).csv"

Comment on @annem-srinivas solution: If you use schemas other than the default (dbo), change the following in his script:评论@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"

and

$queryData = "SELECT * FROM [$($Row[0])].[$($Row[1])]"
$extractFile = "C:\temp\export\$($Row[0]).$($Row[1]).csv"

1st Way第一种方式

You can replicate the schema to a temp database and then use that database to export all tables (along with column names).您可以将架构复制到临时数据库,然后使用该数据库导出所有表(以及列名)。

Steps:脚步:

1) First, create a script for all the tables: 1)首先,为所有表创建一个脚本:
Tasks->Generate Scripts->all tables->single sql file任务->生成脚本->所有表->单个sql文件

2) Create a dummy database and run this script. 2) 创建一个虚拟数据库并运行此脚本。

3) right click on the dummy database and select tasks->export data-> select source data->select destination as Microsoft Excel and give its path->Execute 3) 右键单击​​虚拟数据库并选择任务->导出数据->选择源数据->选择目标为Microsoft Excel并给出其路径->执行

You'll get a single spreadsheet with all the tables and its columns.您将获得一个包含所有表格及其列的电子表格。

2nd Way第二种方式

Execute below query, it'll give all table names in 1st column along with corresponding column names in 2nd column of result.执行下面的查询,它将给出第一列中的所有表名以及结果的第二列中相应的列名。

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

Copy this result and paste it in CSV.复制此结果并将其粘贴到 CSV 中。

Simple one line solution:简单的一行解决方案:

Open Microsoft SQL Server Management studio (SSMS), connect to your DB and run the following:打开 Microsoft SQL Server Management studio (SSMS),连接到您的数据库并运行以下命令:

SELECT 'sqlcmd -s, -W -Q "set nocount on; select * from ['+DB_NAME()+'].[dbo].[' + st.NAME + ']" | findstr /v /c:"-" /b > "c:\target\' +st.NAME +'.csv'  
FROM sys.tables st

create a folder c:\target and open a cmd console in that folder and select & copy all the results of the SQL above and paste them into a cmd window创建文件夹 c:\target 并在该文件夹和 select 中打开一个 cmd 控制台并复制上面 SQL 的所有结果并将它们粘贴到 cmd window

that's it, all the csv files will be created there.就是这样,所有 csv 文件都将在那里创建。

The csv files will be generated each with the first line as the column names将生成 csv 个文件,每个文件的第一行作为列名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM