【问题标题】:Insert file with name containing yesterday's Date using BCP使用 BCP 插入名称包含昨天日期的文件
【发布时间】:2016-02-17 12:05:59
【问题描述】:

我有一个 BCP 命令,它有一个硬编码的文件名 ID_Customer_160216.csv。文件名以日期结尾,格式为yymmdd

bcp sfnav.dbo.Customer in "C:\Users\TSL\Desktop\TSL Data\ID_Customer_151124.csv" -F2 -c -t "^" -r "\n" -S ftpserver\sqlexpress -U abc -P xyz

我想让它动态化:用给定格式的昨天日期替换它。

【问题讨论】:

  • 你在哪里运行这个命令...在 Windows 中的命令行,或者通过使用存储过程 xp_cmdshell 执行命令从 SQL Server 运行?
  • 来自 Windows 的命令行

标签: sql-server windows batch-file command-line bcp


【解决方案1】:

启动 Powershell ISE 并粘贴以下内容。我认为这有点像你想要的......?我知道它不是 cmd,但是,无论如何,是时候从那种事情继续前进了;)

$yesterdaysDateExtension = (Get-Date).AddDays(-1).ToString("yyMMdd")
$fileName = "ID_Customer_$yesterdaysDateExtension.csv"

$filePath = "C:\Users\TSL\Desktop\TSL Data\$fileName"

Write-Host "Attempting bcp with file $filePath"
bcp sfnav.dbo.Customer in $filePath -F2 -c -t "^" -r "\n" -S ftpserver\sqlexpress -U abc -P xyz

【讨论】:

    【解决方案2】:

    一种稳健的方式:

    RunBCP.bat

    @echo off
    
    ::Creating the VBS code that give yesterday date
    echo wscript.echo DateAdd("d", -1, date(^)^)>Day.vbs
    
    ::Getting yesterday date with day.vbs
    for /f "tokens=1-3 delims=/" %%a in ('cscript //nologo Day.vbs') do set "$date=%%c%%b%%a"
    del Day.vbs 2>nul
    
    ::setting date to YYMMDD
    set "$Date=%$Date:~2%"
    
    ::Running BCP with the substitued Date
    bcp sfnav.dbo.Customer in "C:\Users\TSL\Desktop\TSL Data\ID_Customer_%$Date%.csv" -F2 -c -t "^" -r "\n" -S ftpserver\sqlexpress -U abc -P xyz
    

    您只需运行 RunBCP.bat

    【讨论】:

      猜你喜欢
      • 2020-01-09
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2022-12-24
      • 1970-01-01
      • 2019-09-29
      • 2012-02-26
      相关资源
      最近更新 更多