我在這個sql-server查詢中使用IF
語句時遇到問題。sql-server,SQL查詢中的IF語句
我想要什麼:
如果salesHeader.[Ship-to Name]
不爲空,我需要返回,作爲DeliveryName,如果爲空,否則返回的東西。
有沒有辦法在查詢中使用if
語句來做到這一點?
SELECT
poHeader.No_ AS PONumber,
poHeader.[Buy-from Vendor No_] AS VendorNumber,
poHeader.[Document Date] AS DocDate,
salesHeader.GiftMessage,
salesHeader.[Delivery Comment] AS DeliveryComment,
salesHeader.[Shipment Date] AS DeliveryDate,
IF salesHeader.[Ship-to Name] IS NOT NULL
salesHeader.[Ship-to Name] AS DeliveryName
ELSE
poHeader.[Ship-to Name] AS DeliveryName
END
FROM
dbo.[Crocus Live$Purch_ orders for e-mailing] AS poForEmailing
LEFT OUTER JOIN
dbo.[Crocus Live$Purchase Header] AS poHeader ON poForEmailing.No_ = poHeader.No_
INNER JOIN
dbo.[Crocus Live$Vendor] AS vendor ON poHeader.[Buy-from Vendor No_] = vendor.No_
LEFT OUTER JOIN
dbo.[Crocus Live$Sales Header] AS salesHeader ON salesHeader.No_ = dbo.fnGetSalesOrderNumber(poHeader.No_)