我正在尝试使用bcp导出.txt文件:
EXEC xp_CMDSHELL 'BCP "SELECT v.ItemId
, v.InventLocationId
, CAST(v.AvailPhysical AS int)
FROM D_R1.dbo.vwStockOnHand AS v
JOIN D_R1.DBO.vwProducts AS m
ON v.ItemId = m.ITEMID
WHERE NOT EXISTS
(SELECT 1
FROM D_R1.DBO.ExportExcludedFamilies AS magExport
WHERE magExport.REFRECID = m.FamilyRecId)
AND AVAILPHYSICAL > 0 AND v.PICKFROMZ = 1
ORDER BY v.InventLocationId, v.ITEMID"
queryout "C:\temp\1.txt" -c -t"|" -T -S D_R1'
一切都很好,但我需要文件中的最后一行,例如“END OF FILE”,我无法弄清楚..
有人可以给我一个暗示吗?
答案 0 :(得分:1)
1)第一个解决方案
SELECT CONVERT(VARCHAR(11), v.ItemId) AS ItemId
, v.InventLocationId
, CAST(v.AvailPhysical AS int)
, 1 AS Priority
FROM ...
UNION ALL
SELECT 'End of export file', NULL, NULL, 2 AS Priority
ORDER BY Priority, InventLocationId, ItemID
或
SELECT... ;
SELECT 'End of report' ;
或
SELECT... ;
PRINT 'End of report' ;
2)第二个解决方案
bcp "the same SQL query", ...
echo End of export