2017-07-31 64 views
0

我想將我的SSMS 2014查詢結果導出到固定長度的.txt文件,但我不知道如何處理硬編碼(-1)值在我的一個專欄中。嘗試從查詢結果創建制表符分隔的TXT文件在SSMS 2014

任何幫助/方向將不勝感激。這是我的第一次嘗試,但我無法超越這個錯誤。謝謝。

以下是錯誤:

Msg 102, Level 15, State 1, Line 3 
Incorrect syntax near '-'. 

下面是代碼:

xp_cmdshell 'bcp 
"SELECT DISTINCT 
    LEFT(LTRIM(RTRIM('**-1**')), 2) as 'school key', 
    LEFT(LTRIM(RTRIM('1')), 1) as 'district key', 
    LEFT(SPACE(1), 1), 
    LEFT(LTRIM(RTRIM('1')), 1) as 'district wide vendor', 
    LEFT(SPACE(1), 1), 
    LEFT(LTRIM(RTRIM(a_vendor_name)), 30) as name, 
    LEFT(LTRIM(RTRIM(v_vend_address1)), 30) as addr1, 
    LEFT(LTRIM(RTRIM(v_vend_address2)), 30) as addr2, 
    LEFT(LTRIM(RTRIM(v_vend_city)), 25) as city, 
    LEFT(LTRIM(RTRIM(v_vend_state)), 2) as st, 
    LEFT(LTRIM(RTRIM(v_vend_zip)), 10) as zip, 
    LEFT(LTRIM(RTRIM(v_contact1_phone)), 14) as phone, 
    LEFT(LTRIM(RTRIM(v_contact1_fax)), 14) as fax, 
    LEFT(LTRIM(RTRIM(v_vend_status)), 1) as 'status', 
    CASE 
     WHEN LEFT(LTRIM(RTRIM(v_1099_vendor)), 1) = 'Y' THEN '1' ELSE '0' 
    END as irs1099, 
    LEFT(SPACE(1), 1), 
    LEFT(SPACE(10), 10) as tax_id, 
    LEFT(SPACE(6), 6) as vcode, 
    LEFT(LTRIM(RTRIM(v_email_addrs)), 50) as email, 
    LEFT(SPACE(9), 9) as ssn, 
    LEFT(SPACE(20), 20) as comment, 
    LEFT(LTRIM(RTRIM(vr_vend_seq_addr1)), 30) as poaddr1, 
    LEFT(LTRIM(RTRIM(vr_vend_seq_addr2)), 30) as poaddr2, 
    LEFT(LTRIM(RTRIM(vr_vend_seq_city)), 25) as pocity, 
    LEFT(LTRIM(RTRIM(vr_vend_seq_state)), 2) as post, 
    LEFT(LTRIM(RTRIM(vr_vend_seq_zip)), 10) as pozip, 
    LEFT(LTRIM(CONCAT('MUNIS', SPACE(5))), 10) as createdBy, 
    LEFT(LTRIM(RTRIM(REPLACE(CONVERT(varchar(10), getdate(), 101), '/', ''))), 8) as strDate, 
    CASE 
     WHEN LTRIM(RTRIM(a_vendor_remit_seq)) > 0 THEN LEFT(LTRIM(RTRIM(CONCAT(a_vendor_number, a_vendor_remit_seq))), 20) ELSE LEFT(LTRIM(RTRIM(a_vendor_number)), 20) 
    END as covennbr, 
    LEFT(LTRIM(RTRIM(v_dba)), 30) as dba 
FROM dbo.vend_table 
WHERE v_vend_status = 'A' 
AND v_general_type IN ('', '1', '2', '7', '13', '15', '19')" queryout "C:\temp\testTabDelimitedFile.txt -T' 

回答

1

你封裝單引號的xp_cmdshell的命令,同時還採用了在你的價值觀單引號。

例如:

xp_cmdshell 'bcp 
"SELECT DISTINCT 
    LEFT(LTRIM(RTRIM('**-1**')), 2) as 'school key', 
        ^ ^  ^  ^

正因爲如此,你會得到「不正確的語法附近‘ - ’」,因爲xp_cmdshell的認爲**-1**是你通過串獨立。

我會想象你可以逃脫你的單引號(\')。

+0

非常感謝羅比。感謝你的幫助。 – Melinda

相關問題