1. 程式人生 > >Ps—導出:sql結果為csv文件

Ps—導出:sql結果為csv文件

query pre Oday 報表 repo 需求 report 當前 today

 1 $dateText=Get-Date #獲取當前日期時間
 2 $dateText = $dateText.ToShortDateString()  #轉為短日期格式(去掉時間部分)
 3 $checkDate=(Get-Date 0).AddYears((Get-Date).Year - 1).AddMonths((Get-Date).Month).AddDays(-2).ToShortDateString()#當月倒數第2天,轉為短日期格式(去掉時間部分)
 4 if ($dateText -eq $checkDate)
 5 {
 6     $file = Get-ChildItem  -name * -include $dateText
7 if ( $file.count -eq $null ) # 井號是註釋符, $null是預定義的變量,代表空 8 { 9 new-item -path D:/wwwroot/iot_cn/reportfiles -name $dateText -type directory 10 } 11 $today=‘D:\wwwroot\iot_cn\reportfiles\‘+$dateText+‘\query.csv‘ # D:\wwwroot\iot_cn\reportfiles 12 Invoke-Sqlcmd -Query "
use ManuLife; exec sp_consignment_pro_details " -QueryTimeout 3 | Export-Csv $today -NoTypeInformation -Encoding Default 13 }

註:工作中一些小需求,需要定時導出csv報表(頻率不高),寫windows服務感覺太麻煩,在網上搜索,偶然間看到了一些有關“PowerShell”的博文,其間收獲頗豐,頓時油然而生-》“由此強大的工具,豈有不用之理”。

還有其他的辦法導出麽,不要吝嗇哦?

Ps—導出:sql結果為csv文件