如何将SQL查询结果保存到制表符分隔的文本文件

时间:2017-05-30 02:23:24

标签: sql-server tsql export

我正在使用以下SQL脚本生成4个输出查询。我想知道是否有办法将结果直接保存到制表符文本分隔文件。

例如:SELECT * FROM <table> Export TO "C:\TEMP\Output.txt"

由于

DECLARE @DLNK_POS As TABLE
(
    TERMINAL int,
    OPERATOR int,
    TRANSNUM int,
    SEQNUM int,
    STORE_NO int,
    DL_NO int,
    DL_SDATE date,
    DL_EDATE date,
    POS_DATE date,
    PALLET varchar(1),
    ODS varchar(1),
    UPC bigint,
    PACK int,
    SIZE varchar(10),
    ITEM_DESC varchar(40),
    P_BB_AMT decimal(10,2),
    S_BB_AMT decimal(10,2),
    SOLD_QTY int,
    PRICE money,
);


WITH [CTE] As
(
    SELECT
        F1.TERMINAL,
        F1.OPERATOR,
        F1.TRANS_NO,
        F1.SEQ_NO,
        F1.STORE_NO,
        FDL.DL_NO,
        FDL.DL_SDATE1,
        FDL.DL_EDATE1,
        F1.POS_DATE,
        FDL.PALLET,
        FDL.ODS,
        F1.UPC,
        FDL.DL_PACK,
        FDl.DL_SIZE,
        FDL.DL_ITEM_DESC,
        FDL.P_BB_AMT,
        FDL.S_BB_AMT,
        F1.QTY,
        F1.PRICE,
    FROM [MSTR_DB].[dbo].[POS] As F1
    INNER JOIN [MSTR_DB].[dbo].[DEAL] As FDL
    ON (F1.UPC = FDL.DL_UPC)  And (F1.POS_DATE BETWEEN FDL.DL_SDATE1 And FDL.DL_EDATE1) And FDL.S_BB_AMT != 0
)

INSERT  INTO @DLNK_POS SELECT * FROM [CTE];
  

/ * OUTPUT 1 * / SELECT * FROM @DLNK_POS;

     

/ * OUTPUT 2 * / SELECT DL_NO,DL_SDATE,DL_EDATE,PALLET,ODS,   STORE_NO,PACK,SIZE,UPC,ITEM_DESC,P_BB_AMT,S_BB_AMT,   SUM(SOLD_QTY)作为SOLD_QTY来自@DLNK_POS GROUP BY DL_NO,DL_SDATE,   DL_EDATE,PALLET,ODS,STORE_NO,PACK,SIZE,UPC,ITEM_DESC,P_BB_AMT,   S_BB_AMT ORDER BY DL_NO,STORE_NO,UPC;

     

/ * OUTPUT 3 * / SELECT DL_NO,DL_SDATE,DL_EDATE,STORE_NO,   SUM(SOLD_QTY)作为SQL_QTY FROM @DLNK_POS GROUP BY DL_NO,DL_SDATE,   DL_EDATE,STORE_NO ORDER BY DL_NO,STORE_NO

     

/ * OUTPUT 4 * / SELECT DL_NO,DL_SDATE,DL_EDATE,PALLET,ODS,   PACK,SIZE,UPC,ITEM_DESC,P_BB_AMT,S_BB_AMT,SUM(SOL_QTY)As   来自@DLNK_POS GROUP的SOLD_QTY BY DL_NO,DL_SDATE,DL_EDATE,PALLET,   ODS,PACK,SIZE,UPC,ITEM_DESC,P_BB_AMT,S_BB_AMT;

0 个答案:

没有答案