2013-02-20 72 views
1

我需要INSERT數據與日期從一個表到另一個表與Windows批處理(.bat)。mysql與str_to_date插入到批處理

SET thedate='02/02/13' 
mysql -e "INSERT INTO dest select str_to_date($thedate,'%d/%m/%Y') from source" thebase -uroot -ppassword`" 

當我拖動.bat文件到CMD窗口,窗口顯示我的代碼:

SET thedate='02/02/13' 
mysql -e "INSERT INTO dest select str_to_date($thedate,'m/Y') from source" thebase -uroot -ppassword" 

%d/%m/%Y已更改爲m/Y,所以我拿到表DEST空值。當我直接在phpmyadmin中運行這個查詢時,它運行良好。

我用的是XAMPP 18.1阿帕奇/ 2.4.3(Win32的)的OpenSSL/1.0.1c PHP/5.4.7

回答

1

在Windows批次,對於變量的語法是%foo%,不$foo。你可能會用bash混淆它。因此:

  • $thedate不是一個變量
  • %d/%被認爲是一個未設置d/變量
  • %Y%被認爲是一個無與倫比的變量分隔符和被忽略

可以逃脫%如果您複製它:

SET thedate='02/02/13' 
echo %thedate%,'%%d/%%m/%%Y' 

...打印:

'02/02/13','%d/%m/%Y' 

我建議你對通過命令行解釋如果可能的話作曲SQL字符串。很難做到這一點。