我有一個運行並導出CSV文件的SQL cmd。如何與我的查詢中返回的字段進行比較,並根據這些字段在導出中顯示文本?如果它有「標題」部分,我想比較2個字段「First_Unit」和「Unit」。如果他們是平等的,我想添加文本「主要」,如果他們不是那麼我想要「備份」。因此,在本節中,以下是邏輯上的看法:'BWC - '+ UPPER(投訴)+ 'Primary'ELSE'Backup' as Title。因此,我希望在該列中出現在這些數據末尾的兩個文本。如何爲sql cmd導出嵌入if語句
感謝 TJ
這裏是原始查詢:
sqlcmd -S PDCAD -d Ver2_CAD -E -Q "select Active_Calls.Call_Number AS 'Event ID',Officer_IDs AS 'Officer Badge ID', Date_Received AS 'Officer Dispatched DateTime',Time_Complete AS 'Officer Cleared DateTime',Active_Calls.Call_Number AS 'Report Number',UPPER(Complaint) AS 'Category', '' as Tag,'BWC - ' + UPPER(Complaint) as Title,Actual_Incid_Location AS 'Street',Actual_Incid_City AS 'City', ISNULL(Actual_Incid_State,'CT') AS 'State', RIGHT('0'+CONVERT(VARCHAR,Actual_Incid_ZIP),5) AS 'ZIP Code' from Active_Calls INNER JOIN CALLUNITDATA CALLUNITDATA ON Active_Calls.Call_Number=CALLUNITDATA.Call_Number WHERE Officer_IDs <> 'NHFD' AND Officer_IDs <> 'POST53' AND Officer_IDs <> 'POST54' AND Officer_IDs <> 'POST52' AND Officer_IDs <> 'DFD' AND Officer_IDs <> 'NFD' AND Officer_IDs <> 'DFD3' AND Officer_IDs <> 'NHFD2' AND Officer_IDs <> 'UNIT50' AND Officer_IDs <> 'NFD2' AND Officer_IDs <> 'DFD2' AND Officer_IDs <> '1129'AND Officer_IDs <> '2120' AND Officer_IDs <> '0341' AND Officer_IDs <> 'MOB.UNIT51' AND Officer_IDs <> '3540' AND Date_Received >= DATEADD(day, -4, GETDATE()) ORDER BY Date_Received" -o "C:\TaserExportFolder\MyData.csv" -h 852544 -s"," -w 700 -W
我試圖使用CASE語句,但它導致了一個錯誤或者在其中。 – moore1979
你的CASE是什麼樣的,錯誤是什麼? –
問題是我沒有在()中包含CASE語句,所以它在處理查詢時出錯。我不記得確切的錯誤是什麼。 – moore1979