Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Kick Start My Brain
Message
From
18/02/2011 06:54:28
 
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
01500443
Message ID:
01500676
Views:
34
>>
SET ANSI_NULLS ON
>>GO
>>SET QUOTED_IDENTIFIER ON
>>GO
>>-- =============================================
>>-- Author:		<Author,,Name>
>>-- Create date: <Create Date, ,>
>>-- Description:	<Description, ,>
>>-- =============================================
>>CREATE FUNCTION GetDatePaidTo
>>(
>>	-- Add the parameters for the function here
>>	@PolicyHeaderPK uniqueidentifier
>>)
>>RETURNS datetime
>>AS
>>BEGIN
>>	-- Declare the return variable here
>>	DECLARE @DatePaidTo datetime
>>
>>	-- Add the T-SQL statements to compute the return value here
>>	declare @MonthsToAdd int
>>	declare @PremiumMode varchar(15)
>>	declare @IssueDate datetime
>>	
>>	SELECT @MonthsToAdd = SUM(
>>		CASE [PremiumMode]  -- Add table alias here
>>			 WHEN 'Monthly' THEN NumberOfPremiums * 1 -- add table alias for NumberOfPremiums
>>			 WHEN 'Quarterly' THEN NumberOfPremiums * 3
>>			 WHEN 'Semi-Annually' THEN NumberOfPremiums * 6
>>			 WHEN 'Annually' THEN NumberOfPremiums * 12
>>			 ELSE 0
>>		END )
>>	FROM [dbo].[ReceiptDetails]
>>	inner join [dbo].[Receipts] on ReceiptFK = ReceiptPK  -- looks like this join is not needed, also, add aliases to tables
>>	WHERE 
>>		([PolicyHeaderFK] = @PolicyHeaderPK
>>		AND ReceiptTypeFK = 3)
>>
>>	SELECT @IssueDate = IssueDate -- add alias here
>>	FROM [dbo].[Policyheaders]
>>		INNER JOIN [dbo].[Policies]	ON [PolicyHeaderPK] = [PolicyHeaderFK]
>>		INNER JOIN (SELECT [PolicyHeaderFK], MAX(LastChanged) as LastChanged
>>						FROM [dbo].[Policies]
>>						GROUP BY [PolicyHeaderFK]) subquery ON [Policies].[PolicyHeaderFk] = subquery.policyHeaderFK AND Policies.LastChanged = subquery.LastChanged
>>		inner join [dbo].[Plans] on [PlanFK] = [PlanPK] -- do we need all these joins?
>>		inner join [dbo].[Clients] on [ClientFK] = [ClientPK]
>>		INNER JOIN [dbo].[Status] on [StatusFK] = [StatusPK]
>>	WHERE 
>>		([PolicyHeaderPK] = @PolicyHeaderPK)
>>
>>	SET @DatePaidTo = DATEADD(M, @MonthsToAdd, @IssueDate)
>>	
>>	-- Return the result of the function
>>	RETURN @DatePaidTo
>>
>>END
>>GO
>>
>>Thanks
>
>I put some comments. It may be OK. Keep in mind that scalar functions in T-SQL may be slow.
>Also, I suggest to add table alias for each field, as if someone needs to maintain this code, it's not clear which table which field belongs to.

Hi again Naomi,

how do I use this function in a computed field now? I've added the field to the table and put GetDatePaidTo(PolicyHeaderPK) as the formula but it tells me "Error validating the formula for the column" when I try to save the table definition.
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform