Reputation: 1
Looking to write a function where I can call from any two columns in my table, to get the resulting column in the same table. Table: Acme Columns: CustomerID, LastName, FirstName, EmailAdress, MailingAddress, City, State, Zip
Can I get EmailAddress from just FirstName and Last Name? Can I get Zip from CustomerID and City?
What I have so far for EmailAddress:
CREATE FUNCTION fnEmailAddress
(@LastName varchar (255), @FirstName varchar (255))
RETURNS table
RETURN (SELECT EmailAddress
FROM Acme
WHERE FirstName = @FirstName AND LastName = @LastName);
END
So the following would give me [email protected]: EXEC fnEmailAddress ('Brenda'+'Chen')
But it doesn't work :(
Upvotes: 0
Views: 734
Reputation: 752
If I understand you correctly, you want a function that can adapt to several possibilities of input combinations, because what columns you will have ready as input might differ in each function call. If so:
Lastly, choose one of either:
NULL
, then query once with a WHERE
clause that auto-passes any NULL
inputs (example: WHERE COL1 = ISNULL(@PAR1, COL1) AND COL2 = ISNULL(@PAR2, COL2)
, needs more logic for NULL
able columns),IF
/ELSE
commands to switch between several different queries (different queries is probably maintenance headache, so avoid when possible).Below is an example based on something I already have, although it uses the IF
/ELSE
approach. Try to do NULL
/ISNULL
instead:
CREATE FUNCTION [dbo].[SomeWackoName]
(@FirstArm tinyint, @SecondArm tinyint, @FirstLeg tinyint, @SecondLeg tinyint)
RETURNS @Results TABLE(
[Value] varchar(60)
) AS
BEGIN
-- For input, we need both arms, or both legs. It is acceptable to provide three inputs.
-- It is unacceptable to provide all four inputs (confusing), or failing to provide any full pair (like one input, or two unpaired inputs).
DECLARE @Validator tinyint = CASE WHEN @FirstArm + @SecondArm IS NULL THEN 0 ELSE 1 END + CASE WHEN @FirstLeg + @SecondLeg IS NULL THEN 0 ELSE 2 END
IF @Validator NOT BETWEEN 1 AND 2 RETURN --THROW 50000, 'INCORRECT INPUT WHYYYYYYYYYYYYYYY WHYYYYYYYYYYYYYYYYYY', 0;
-- Depending on input provided, decide how to behave.
IF @Validator = 1
INSERT INTO @Results SELECT 'I GOT THE ARMS. POPULATE WITH LEGS!!'
ELSE IF @Validator = 2
INSERT INTO @Results SELECT 'I GOT THE LEGS. POPULATE WITH ARMS!!'
-- Return results.
RETURN
END
Upvotes: 1
Reputation: 199
CREATE FUNCTION fnEmailAddress
(
@LastName varchar (255)=null, @FirstName varchar (255)=null,
@streetNumber varchar(255)=null,@streetname varchar (255)=null
)
RETURNS table
Select case when @firstname+@lastname is not null then EMail Else LastName End Column3
from person
where
(
case when @firstname+@lastname is not null
then case when FirstName=@firstname and LastName=@lastname
then 1 else 0 End
when @StreetNumber+@streetname is not null
then case when StreetName=@Streetname and StreetNumber=@streetNumber
then 1 Else 0 End
End
)=1
The function should have 4 input parameters, pass first two or the last two parameters only.
CASE in the select statement will get 'Email' or 'LastName' depending upon input parameter null or not.
Similarly, CASE in the where statement will choose which filter should apply depending upon input parameter null or not, and results will be fetched based on that.
Upvotes: 0