我第一個想到的就是直接再報表裡面寫CommandText丟SQL進去撈資料,但報表的程式碼看起來非常大一串
後來前輩叫我用SQL-SERVER的Function,想說T-SQL不熟就來練練吧
具參數的"內嵌資料表值"函數 (INLINE TABLE-VALUED FUNCTION)
CREATE FUNCTION INVTEST (@A varchar(20),
@B varchar(20),
@C varchar(20))
RETURNS TABLE
AS
RETURN
SELECT X.column1 ,
Y.column1 ,
CAST(ISNULL(X.column2,0) AS numeric(10,2)) ,
CAST(ISNULL(Y.column2,0) AS numeric(10,2))
FROM TABLE_NAME_1 X
LEFT JOIN TABLE_NAME_3 Y
ON ...
AND ...
WHERE X.column5=@A
AND X.column6=@B
AND X.column7=@C
UNION ALL
SELECT X.column ,
Y.column ,
CAST(ISNULL(X.column,0) AS numeric(10,2)) ,
CAST(ISNULL(Y.column,0) AS numeric(10,2))
FROM TABLE_NAME_2 X
LEFT JOIN TABLE_NAME_3 Y
ON ...
AND ...
AND ...
WHERE X.column5=@A
AND X.column6=@B
AND X.column7=@C
--CAST(ISNULL(X.column2,0) AS numeric(10,2)) ,
-- 處理如果X.column2是空值,就帶0,再將處理完的值轉成數值 : 整數位8碼,小數位2碼
--UNION ALL上面跟下面的主要TABLE不一樣
--@A , @B , @C 要丟進去查詢的值
;
執行
SELECT * FROM dbo.INVTEST(A,B,C)