Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need a fucntion that takes in three variables and checks three tables

@LNumber varchar(max),
@OType varchar(max),
@CDate datetime

T1.number varchar(max)
T1.date datetime
T1.ctps int

T2.number varchar(max)
T2.addedDate date
T2.RemovedDate date
T2.ctps int

T3.OType varchar(max)
T3.LNumber varchar(max)

VB
If @LNumber = T1.Number
if @OType = 'LTD' or 'PLC'
if T1.CTPS = 1
if @CDate  < T1.Date
return "No"

if @LNumber = T1.Number
if @OType = 'LTD' or 'PLC'
if T1.CTPS = 1
if@ CDate  > T1.Date
return "Yes"
------------------------------------------------------------------------
If @LNumber = T1.Number
if @OType = 'LTD' or 'PLC'
if T1.CTPS = 0
if @CDate  < T1.Date
return "No"

if @LNumber = T1.Number
if @OType = 'LTD' or 'PLC'
if T1.CTPS = 0
if @CDate > T1.Date
return "No"
------------------------------------------------------------------------------
If @LNumber = T1.Number
if @OType != 'LTD' or 'PLC'
if T1.CTPS = 1
if @CDate < T1.Date
return "No"

if @LNumber = T1.Number
if @OType != 'LTD' or 'PLC'
if T1.CTPS = 1
If @CDate > T1.Date
return "Yes"
-------------------------------------------------
If @LNumber = T1.Number
if @OType != 'LTD' or 'PLC'
if T1.CTPS = 0
if @CDate < T1.Date
return "No"

if @LNumber = T1.Number
if @OType != 'LTD' or 'PLC'
if T1.CTPS = 0
if @CDate > T1.Date
return "Yes"
--------------------------------------------------
If @LNumber = T2.Number
If @OType = 'LTD' or 'PLC'
If T2.CTPS = 0
If @CDate BETWEEN T2.DateAdded AND T2.DateRemoved
return "No"

If @LNumber = T2.Number
If @OType = 'LTD' or 'PLC'
If T2.CTPS = 0
If @CDate NOT BETWEEN T2.DateAdded AND T2.DateRemoved
return "No"

------------------------------------------------------
If @LNumber = T2.Number
If @OType = 'LTD' or 'PLC'
If T2.CTPS = 1
If @CDate BETWEEN T2.DateAdded AND T2.DateRemoved
return "Yes"

If @LNumber = T2.Number
If @OType = 'LTD' or 'PLC'
If T2.CTPS = 1
If @CDate NOT BETWEEN T2.DateAdded AND T2.DateRemoved
return 

------------------------------------------------------
If @LNumber = T2.Number
if @OType != 'LTD' or 'PLC'
if T2.CTPS = 1
if @CDate BETWEEN T2.DateAdded AND T2.DateRemoved
return "Yes"

if @LNumber = T2.Number
if @OType != 'LTD' or 'PLC'
if T2.CTPS = 1
if @CDate NOT BETWEEN T2.DateAdded AND T2.DateRemoved
return "No"
--------------------------------------------------
If @LNumber = T2.Number
if @OType != 'LTD' or 'PLC'
if T2.CTPS = 0
if @CDate BETWEEN T2.DateAdded AND T2.DateRemoved
return "Yes"

if @LNumber = T2.Number
if @OType != 'LTD' or 'PLC'
if T2.CTPS = 0
if @CDate NOT BETWEEN T2.DateAdded AND T2.DateRemoved
return "No"
--------------------------------------------------
Posted
Updated 10-Jul-13 4:00am
v2
Comments
ZurdoDev 10-Jul-13 10:05am    
What's the question?
Love Toys 10-Jul-13 10:11am    
I am very new to sql functions and the help i have found on line is no help so wanted someone to help me build a fucnction that takes in the above three variables and return the answer when each of the different criteria is met i.e: If @LNumber = T2.Number
If @OType = 'LTD' or 'PLC'
If T2.CTPS = 1
If @CDate BETWEEN T2.DateAdded AND T2.DateRemoved
ZurdoDev 10-Jul-13 10:14am    
In SQL management studio, right click functions, and choose New Multi-statement table valued function. Or create a Stored Procedure the same way. It will create the template for you.
Love Toys 10-Jul-13 10:17am    
no help what so ever
Love Toys 10-Jul-13 10:17am    
its syntax im after and how it would be wrote

1 solution

Any code with that many open and close brackets is going to be a nightmare to untangle!
First thing you need to do is simplify your conditions ...
For example if @OType != 'LTD' or 'PLC' could be better expressed as
if @OType NOT IN ('LTD','PLC')

Next @CDate can only be BETWEEN T2.DateAdded AND T2.DateRemoved or not - there are no other options. So you can simplify
VB
If @LNumber = T2.Number
if @OType != 'LTD' or 'PLC'
if T2.CTPS = 1
if @CDate BETWEEN T2.DateAdded AND T2.DateRemoved
return "Yes"

if @LNumber = T2.Number
if @OType NOT IN ('LTD','PLC')
if T2.CTPS = 1
if @CDate NOT BETWEEN T2.DateAdded AND T2.DateRemoved
return "No"

To become
VB
If @LNumber = T2.Number
if @OType != 'LTD' or 'PLC'
if T2.CTPS = 1
if @CDate BETWEEN T2.DateAdded AND T2.DateRemoved
return "Yes" else return "No"

Finally, consider building a "Truth Table" (google it if you're not sure) to look at all of your conditions - sometimes you will spot patterns that make the whole thing simpler and easier to read. You may also spot the conditions you haven't catered for ... such as what to do if @CDate == T1.Date and the fact you have a return which returns nothing at all.

Give that a go and hopefully you'll be able to solve the problem yourself
 
Share this answer
 
Comments
Love Toys 11-Jul-13 8:11am    
THIS IS MY ORIGINAL QUERY I BUILT BUT NEED IT CHANGING INTO A FUNCTION:|

DECLARE @StartDate DATE = '01/04/2013'
DECLARE @EndDate DATE = '30/04/2013'

SELECT ContractID
,LineNumber
,ContractDate
,[Date]
,o.Name
,u.[File]
,DateAdded
,DateRemoved
,u.CTPS
,B.Name [Branch Name]

END
INTO [TPS]
FROM [Contract] c
INNER JOIN [Account] a
ON [c].[AccountFK] = [a].[AccountID]
INNER JOIN [Profile] p
ON [p].[ProfileID] = [a].[ProfileFK]
INNER JOIN [Line] l
ON [l].[ContractFK] = [ContractID]
LEFT JOIN [Deal] d
ON [c].[ContractID] = [d].[ContractFK]
INNER JOIN ( SELECT BranchTypeFK, BranchID, Name
FROM [Branch] b
)b
ON [d].[BranchFK] = [b].[BranchID]
AND BranchTypeFK IN (1,2,4)
INNER JOIN [OrganisationType] o
ON [p].[OrganisationTypeFK] = [o].[OrganisationTypeID]
LEFT JOIN (
SELECT [CLI], [Date] [lDate], [Date], [CTPS], [File] = 'Lookup'
FROM [Lookup] tl
)u
ON u.CLI = l.LineNumber
LEFT JOIN (
SELECT [CLI], [DateAdded], [DateRemoved], [CTPS], [File] = 'History'
FROM History h
)h
ON h.CLI = l.LineNumber
WHERE ContractDate BETWEEN @StartDate AND @EndDate

SELECT *
,[OnTPS] = CASE
WHEN ([Date] IS NOT NULL AND [ContractDate] < [Date]) THEN 'No'
WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date]) THEN 'Yes'

WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved]) THEN 'Yes'
WHEN ([Date] IS NULL AND [ContractDate] NOT BETWEEN [DateAdded] AND [DateRemoved]) THEN 'No'

WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 1) THEN 'Yes'
WHEN ([Date] IS NULL AND [ContractDate] BETWEEN [DateAdded] AND [DateRemoved] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 0) THEN 'No'

WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 1) THEN 'Yes'
WHEN ([Date] IS NOT NULL AND [ContractDate] > [Date] AND [Name] = 'PLC' OR [Name] = 'LTD' AND CTPS = 0) THEN 'No'
END
FROM [Temp].[dbo].[TPS] t
CHill60 11-Jul-13 8:28am    
So are you saying you are now getting the results you expect in all instances but want to turn this into a function ready to implement for real?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900