Question : SQL Functions

I have a function that returns a table to my VB program.  I need to add conditions to this function depending on what the user selects in their search.

Here are the conditions:

        'See what episodes the user wants to view
        If msEpisodeState = "O" Then
            sSQL = sSQL & "AND e.active = 1 AND (dc.DischargeDate IS NULL OR e.date_end IS NULL) " & _
                "AND dc.countersigned_date IS NULL AND dc.signed_date IS NULL "
        ElseIf msEpisodeState = "U" Then
            sSQL = sSQL & "And c.EndDate IS NULL " & _
                "AND c.ID NOT IN (SELECT c.ID FROM Clients c INNER JOIN Episodes e ON(c.ID = e.client_id) " & _
                "Where c.AgencyID = " & CStr(mnAgencyID) & " AND e.active = 1 AND e.date_end IS NULL) "
        ElseIf msEpisodeState = "R" Then
            sSQL = sSQL & "AND e.active = 0 AND (dc.DischargeDate IS NULL AND e.date_end IS NULL) "
        ElseIf msEpisodeState = "P" Then
            sSQL = sSQL & "AND e.active = 1 AND (dc.DischargeDate IS NOT NULL OR e.date_end IS NOT NULL) " & _
                    "AND dc.signed_date IS NOT NULL AND dc.countersigned_date IS NULL "
        ElseIf msEpisodeState = "D" Then
                sSQL = sSQL & "AND e.active = 0 AND (dc.DischargeDate IS NOT NULL OR ct.date_end IS NOT NULL) " & _
                    "AND dc.signed_date IS NOT NULL AND dc.countersigned_date IS NOT NULL "
        End If


I currently have these conditions in the VB program but I need to move them to the Function.

Any help is appreciated.

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
ALTER FUNCTION [dbo].[fCaseLoadRU](@agencyId bigint, @PersonnelId bigint = null, @ReportingUnit varchar(40) = null)
RETURNS TABLE
AS
RETURN (
		SELECT ID as ClientID, ProviderNumber, 
		CASE WHEN a.RUCount = 1 THEN ProviderNumber ELSE 'YES: ' + CAST(a.RUCount AS VARCHAR(10))END as RUCount
		FROM 
		(	SELECT c.ID, RUCount= COUNT(*), ProviderNumber = MAX(e.ReportingUnit)
			FROM Clients c 
			INNER JOIN Episodes e ON(c.ID = e.client_id)
			LEFT JOIN ru repUnit ON(e.eRuId= repUnit.id) 
			INNER JOIN (SELECT * FROM UnitAllocation WHERE [PersonnelId] = @PersonnelId AND AccessToClientData =1) ua ON ReportingUnitID = repUnit.ID 
			LEFT JOIN (select * from fDischargeClientActive(@agencyId)) as dc ON (EpisodeID = e.episode_id)
			WHERE  c.agencyid = @agencyId
			AND repUnit.reportingunit = ISNULL(@ReportingUnit, repUnit.reportingunit)
			GROUP BY c.ID
			
			UNION ALL
			
			SELECT c.ID, RUCount= COUNT(*), ProviderNumber = MAX(e.ReportingUnit)
			FROM Clients c 
			INNER JOIN Episodes e ON(c.ID = e.client_id)
			LEFT JOIN ru repUnit ON(e.eRuId= repUnit.id) 
			WHERE  c.agencyid = @agencyId  AND c.TempClinicianID = @PersonnelId
			AND repUnit.reportingunit = ISNULL(@ReportingUnit, repUnit.reportingunit)
			GROUP BY c.ID
		)a
)

Answer : SQL Functions

Kindly use procedure with Dynamic SQL approach as given in one of the other question you asked..
Random Solutions  
 
programming4us programming4us