SQL Create View and using it in Function
I have the following function and I need to take out the SELECT part and
create a separate view.
CREATE FUNCTION dbo.dbf_get_penalty_points
( @pn_seq_party_id NUMERIC(18),
@pv_penalty_points_code CHAR(1) = 'Y') -- Use 'N' for total
points, otherwise will return Current Penalty Points
RETURNS NUMERIC(18,0)
AS
BEGIN
DECLARE @n_penalty_points NUMERIC(18),
@d_latest_points_date DATETIME
SELECT @d_latest_points_date = dbo.dbf_trunc_date(DateAdd(mm, -
Abs(Convert(NUMERIC(18,0),dbo.dbf_get_sys_param('CMS2', 'PP_MONTHS'))),
GetDate()))
SELECT @n_penalty_points = IsNull(Sum(penalty_points_amount),0)
FROM dbo.ar_penalty_point WITH(NOLOCK)
WHERE seq_party_id = @pn_seq_party_id
AND 1 = CASE
WHEN @pv_penalty_points_code = 'N' THEN 1
WHEN @pv_penalty_points_code = 'Y' AND added_date >=
@d_latest_points_date AND reset_date IS NULL THEN 1
ELSE 0
END
RETURN @n_penalty_points
END
GO
SET QUOTED_IDENTIFIER OFF
GO
GRANT EXECUTE ON dbo.dbf_get_penalty_points TO standard
GO
I have tried and got this,
SELECT SUM(CASE WHEN added_date >=dbo.dbf_trunc_date(DateAdd(mm, -
Abs(Convert(NUMERIC(18,0),dbo.dbf_get_sys_param('CMS2', 'PP_MONTHS'))),
GetDate()))
AND reset_date IS NULL THEN 1
ELSE 0) current_points,
IsNull(Sum(penalty_points_amount),0) total_points,
seq_party_id
FROM dbo.ar_penalty_point WITH(NOLOCK)
GROUP BY seq_party_id
Now I need to get rid of
dbo.dbf_trunc_date(DateAdd(mm, -
Abs(Convert(NUMERIC(18,0),dbo.dbf_get_sys_param('CMS2', 'PP_MONTHS'))),
GetDate()))
From the SELECT part of the query. I am struck is there a better way to
write my view ?
No comments:
Post a Comment