csv - 如何自动将数据从 SQL Server 2012 导出到 CSV 文件?

标签 csv sql-server-2012 export-to-csv

希望我不会因为问太简单的问题而让任何人感到不安!

我需要将数据从 SQL Server 2012 表导出到 CSV 文件。这需要每小时完成一次,或者理想情况下,如果可能的话,每当创建新记录或更新/删除现有记录时完成。该表包含我们维护的所有站点的列表。我需要将此 CSV 文件导出到特定位置,因为第三方数据库有一个 API 可以监视该位置并从那里导入 CSV 文件。

要从SQL中提取的数据是:

Mxmservsite.siteid as Marker_ID, mxmservsite.name as Name, 'SITE' as Group, '3' as Status, 
'' as Notes, mxmservsite.zipcode as Post_Code, 'GB' as Country, '' as Latitude, 
'' as Longitude, '' as Delete
Where dataareaid='ansa'

有人知道我该怎么做吗?抱歉,我是 SQL 新手,仍在学习基础知识!我过去搜索过类似的问题,但没有找到任何东西。我知道有一个名为 BCP 的实用程序,但不确定这是否是最好的方法,如果是,那么我如何使用它每小时运行一次,或者每当有记录更新/删除/插入时运行?

干杯

最佳答案

这里有一些 powershell 可以满足您的需求;只需使用 Windows 任务计划程序来安排它:

function Execute-SQLQuery {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$DbInstance
        ,
        [Parameter(Mandatory = $true)]
        [string]$DbCatalog
        ,
        [Parameter(Mandatory = $true)]
        [string]$Query
        ,
        [Parameter(Mandatory = $false)]
        [int]$CommandTimeoutSeconds = 30 #this is the SQL default
    )
    begin {
        write-verbose "Call to 'Execute-SQLQuery': BEGIN"
        $connectionString = ("Server={0};Database={1};Integrated Security=True;" -f $DbInstance,$DbCatalog)
        $connection = New-Object System.Data.SqlClient.SqlConnection
        $connection.ConnectionString = $connectionString
        $connection.Open()    
    }
    process {
        write-verbose "`n`n`n-----------------------------------------"
        write-verbose "Call to 'Execute-SQLQuery': PROCESS"
        write-verbose $query 
        write-verbose "-----------------------------------------`n`n`n"
        $command = $connection.CreateCommand()
        $command.CommandTimeout = $CommandTimeoutSeconds
        $command.CommandText = $query
        $result = $command.ExecuteReader()
        $table = new-object “System.Data.DataTable”
        $table.Load($result)
        Write-Output $table
    }
    end {
        write-verbose "Call to 'Execute-SQLQuery': END"
        $connection.Close()
    }
}

Execute-SQLQuery -DbInstance 'myServer\InstanceName' -DbCatalog 'myDatabase' -Query @"
select Mxmservsite.siteid as Marker_ID
 , mxmservsite.name as Name
 , 'SITE' as Group
 , '3' as Status
 , '' as Notes
 , mxmservsite.zipcode as Post_Code
 , 'GB' as Country
 , '' as Latitude
 , '' as Longitude
 , '' as Delete
 From mxmservsite --this wasn't in your original code
 Where dataareaid='ansa'
 "@ | Export-CSV '.\MyOutputFile.csv' -NoType 

任何变化都可以触发某些东西;即您可以在表上创建一个触发器,然后使用 xp_cmdshell执行脚本或类似内容;但这会导致性能问题(如果在没有完全理解的情况下使用触发器通常是一个糟糕的选择)。 xp_cmdshell 也会给您带来一些安全风险。

还有很多其他方法可以实现这一目标;目前我喜欢 PowerShell,因为它可以为您提供大量的灵活性,而且开销很小。

另一个选择可能是考虑使用 linked servers允许您的源数据库直接更新目标,而不需要 CSV。

关于csv - 如何自动将数据从 SQL Server 2012 导出到 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186750/

相关文章:

powershell - 使用 csv 的 ForEach 中的 If 语句

javascript - 在 SVG 中选择坐标处的元素

javascript - csv export with symbol # as content 导出结束

node.js - Nodejs 发送部分响应

sql-server-2005 - 不使用 SSIS 将平面文件导入 SQL Server 2005 的最优雅方法

matlab - 从 textread 更改为 textscan MATLAB

sql - 将排名添加到每组的第一行

sql - 在 SQL Server 数据库中从一张表批量插入到另一张表

ssis - 项目一致性检查失败。与项目的保护级别不同

r - 在 R 中将 .txt 更改为 .csv