2015-10-06 124 views
0

我想使用批處理文件讀取文件,然後使用同一批處理文件中的SQL命令檢索到的數據。當我的路徑不包含空格時,它工作得很好。但是,當我使用具有空格的路徑時,我會收到SQL錯誤。批處理文件中的空間在SQL中引發錯誤

CODE:

FOR /F "delims=" %%i in ("N:\Mica Projects\Lab Reports\acct%mydate%.csv") DO call :concat %%i 
SET acct=%acct:~0,-1% 
bcp "SELECT aVIS.AccountNumber, aVIS.VisitID, aVIS.Name, aVIS.LocationID, aVIS.InpatientOrOutpatient , oORD.OrderDateTime, oORD.OrderNumber, oORD.OrderedProcedure, oORD.OrderedProcedureMnemonic, oORD.OrderedProcedureName, oORD.EnteredUserID , oORD.ServiceDateTime FROM [boringmdb].[dbo].[OeOrders] oORD JOIN [boringmdb].[dbo].[AdmVisits] aVIS ON oORD.VisitID = aVIS.VisitID WHERE oORD.VisitID in (SELECT DISTINCT(VisitID) FROM [boringmdb].[dbo].[AdmVisits] WHERE AccountNumber IN (%acct%)) AND oORD.Category IN ('BBK','LAB','MIC','OVBBK','OVLAB','OVMIC','OVPTH','PTH') ORDER BY aVIS.AccountNumber" queryout "N:\Mica Projects\Lab Reports\Result_Data%mydate%.csv" -c -t, -S MICA-01 -T 
ECHO %acct 

當我運行上面我得到這個:

SQLState = 3700, NativeError = 105 
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server] Unclosed quotation mark after the character string ' 'N:\MICA')) AND oORD.Category IN ('BBK','LAB','MIC','OVBBK','OVLAB','OVMIC','OVPTH','PTH') ORDER BY aVIS.AccountNumber'. 
SQLState = 3700, NativeError = 102 
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near ' 'N:\MICA')) AND oORD.Category IN ('BBK','LAB','MIC','OVBBK','OVLAB','OVMIC','OVPTH','PTH') ORDER BY aVIS.AccountNumber'. 
SQLState = 3700, NativeError = 8180 
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could not be prepared. 

我最有可能缺少明顯。任何人都可以幫忙嗎? 預先感謝您。

+1

也許第一行應該有... DO CALL:concat「%%〜i」 – lit

回答

1

試試這個:

FOR /F "usebackq delims=" %%i in ("N:\Mica Projects\Lab Reports\acct%mydate%.csv") DO call :concat "%%~i" 

沒有usebackq選項的雙引號字符串("")被視爲文本字符串,而不是文件路徑的for /F

我還更改了call參數,因爲它始終用雙引號(~修飾符刪除引號(如果存在))。

+0

謝謝Aschipfl。我非常感謝你的解決方案,它完美的作品。 –