從 TSQL 語法發現到規格資訊(Mt_Nam_Dim)是透過 ISNULL() 來判斷產生,原資料庫的資料是完整的,ISNULL() 回傳值就是問題所在
SELECT
BD.BillNO,
ID.MtCode,
ISNULL(M.Mt_num, D.Mt_Num) AS Mt_Num,
ISNULL(M.Mt_Nam_Dim,D.Spec) AS Mt_Nam_Dim,
ID.RushQty,
ID.RushPrice,
ID.SubTotal
FROM PurchBillDetail AS BD
JOIN PurchPayDetail AS PD ON BD.PNO = PD.PNO AND BD.RowNO = PD.RowNO
JOIN PurchIn AS I ON PD.PINO = I.PINO
JOIN PurchInDetail AS ID ON I.PINO = ID.PINO
JOIN PurchDetail AS D ON ID.PurNO = D.PurNO AND ID.MtCode = D.MtCode
LEFT JOIN MtData AS M ON ID.MtCode = M.MtCode
WHERE BD.BillNO = 'P1040518028'
MSDN ISNULL() 相關資訊從備註中發現到線索,查欄位資料型態長度,發現分別
- 語法:ISNULL ( check_expression , replacement_value )
- 傳回類型:傳回與 check_expression 相同的類型。 如果提供了常值 NULL 做為 check_expression,就會傳回 replacement_value 的資料類型。 如果提供了常值 NULL 做為 check_expression 但是沒有提供任何 replacement_value,就會傳回 int。
- 備註:如果 check_expression 值不是 NULL,便傳回這個值;否則,如果類型不同,便在 replacement_value 隱含地轉換成 check_expression 的類型之後,再傳回它。 如果 replacement_value 比 check_expression 長,replacement_value 可能會被截斷。
- Mt_Nam_Dim:char(100)
- Spec:varchar(500)
修正 TSQL,在 ISNULL() 內強制把第一參數轉型為 varchar(500) 來避免這個問題
SELECT
BD.BillNO,
ID.MtCode,
ISNULL(M.Mt_num, D.Mt_Num) AS Mt_Num,
ISNULL
(
CAST(M.Mt_Nam_Dim AS varchar(500)), -- char(100)
D.Spec -- varchar(500)
) AS Mt_Nam_Dim,
ID.RushQty,
ID.RushPrice,
ID.SubTotal
FROM PurchBillDetail AS BD
JOIN PurchPayDetail AS PD ON BD.PNO = PD.PNO AND BD.RowNO = PD.RowNO
JOIN PurchIn AS I ON PD.PINO = I.PINO
JOIN PurchInDetail AS ID ON I.PINO = ID.PINO
JOIN PurchDetail AS D ON ID.PurNO = D.PurNO AND ID.MtCode = D.MtCode
LEFT JOIN MtData AS M ON ID.MtCode = M.MtCode
WHERE BD.BillNO = 'P1040518028'
下圖為正確顯示- 參考資料
- ISNULL()
沒有留言:
張貼留言