Solved

Default parameter in SQL function

Posted on 2008-06-17
3
1,278 Views
Last Modified: 2008-11-04
I have been tasked to create a report that compares the sum of values from the previous year, the current month and ytd in three columns.  In my first stab at it, I created a ytd function and a monthly function.  I'd like to refine this by creating one function that uses an either default parameter that I either won't use or an optional parameter.  Something like

CREATE FUNCTION dbo.TESTFUNCTION(@PERIOD INT, @YEAR INT, @MONTH INT) <<< I would like @MONTH to be optional.  If @PERIOD = 1 then I'm going for a yearly total and I don't need the month.
0
Comment
Question by:AaronGreene1906
  • 2
3 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21803123
CREATE FUNCTION dbo.TESTFUNCTION(@PERIOD INT, @YEAR INT, @MONTH INT = 0)
0
 

Author Comment

by:AaronGreene1906
ID: 21806505
This is what I have now, but I'm having trouble with the IF...THEN statement.

CREATE FUNCTION dbo.HOURS(@YEAR INT,@MONTH INT = 0)
RETURNS REAL
AS
BEGIN
DECLARE @RESULT REAL
IF @MONTH = 0 THEN
SELECT @RESULT = ISNULL(SUM(X.HOURS),0)
FROM
(SELECT     DATEPART(Yy, dbo.tblWorkOrder.txtWORKSTARTDATE) AS YEAR, DATEPART(Mm, dbo.tblWorkOrder.txtWORKSTARTDATE) AS MONTH,
                      dbo.tblWorkOrder.cboBUNIT AS UNIT, dbo.tblWorkOrder_Labor.intLABOR_CODE AS PAYTYPE, dbo.tblWorkOrder_Labor.txtHOURS AS HOURS
FROM         dbo.tblWorkOrder INNER JOIN
                      dbo.tblWorkOrder_Labor ON dbo.tblWorkOrder.txtWORKORDER = dbo.tblWorkOrder_Labor.txtWORKORDER INNER JOIN
                      dbo.tblData_Employee ON dbo.tblWorkOrder_Labor.txtEMPLOYEEID = dbo.tblData_Employee.txtEMPLOYEEID) X
WHERE
X.YEAR = @YEAR
ELSE
SELECT @RESULT = ISNULL(SUM(X.HOURS),0)
FROM
(SELECT     DATEPART(Yy, dbo.tblWorkOrder.txtWORKSTARTDATE) AS YEAR, DATEPART(Mm, dbo.tblWorkOrder.txtWORKSTARTDATE) AS MONTH,
                      dbo.tblWorkOrder.cboBUNIT AS UNIT, dbo.tblWorkOrder_Labor.intLABOR_CODE AS PAYTYPE, dbo.tblWorkOrder_Labor.txtHOURS AS HOURS
FROM         dbo.tblWorkOrder INNER JOIN
                      dbo.tblWorkOrder_Labor ON dbo.tblWorkOrder.txtWORKORDER = dbo.tblWorkOrder_Labor.txtWORKORDER INNER JOIN
                      dbo.tblData_Employee ON dbo.tblWorkOrder_Labor.txtEMPLOYEEID = dbo.tblData_Employee.txtEMPLOYEEID) X
WHERE X.YEAR = @YEAR
AND X.MONTH = @MONTH
RETURN @RESULT
END
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21806542
You don't need THEN in TSQL:
CREATE FUNCTION dbo.HOURS(@YEAR INT,@MONTH INT = 0)
RETURNS REAL
AS
BEGIN
DECLARE @RESULT REAL
IF @MONTH = 0 
BEGIN
SELECT @RESULT = ISNULL(SUM(X.HOURS),0)
FROM
(SELECT     DATEPART(Yy, dbo.tblWorkOrder.txtWORKSTARTDATE) AS YEAR, DATEPART(Mm, dbo.tblWorkOrder.txtWORKSTARTDATE) AS MONTH,
                      dbo.tblWorkOrder.cboBUNIT AS UNIT, dbo.tblWorkOrder_Labor.intLABOR_CODE AS PAYTYPE, dbo.tblWorkOrder_Labor.txtHOURS AS HOURS
FROM         dbo.tblWorkOrder INNER JOIN
                      dbo.tblWorkOrder_Labor ON dbo.tblWorkOrder.txtWORKORDER = dbo.tblWorkOrder_Labor.txtWORKORDER INNER JOIN
                      dbo.tblData_Employee ON dbo.tblWorkOrder_Labor.txtEMPLOYEEID = dbo.tblData_Employee.txtEMPLOYEEID) X
WHERE
X.YEAR = @YEAR
END
ELSE
BEGIN
SELECT @RESULT = ISNULL(SUM(X.HOURS),0)
FROM
(SELECT     DATEPART(Yy, dbo.tblWorkOrder.txtWORKSTARTDATE) AS YEAR, DATEPART(Mm, dbo.tblWorkOrder.txtWORKSTARTDATE) AS MONTH,
                      dbo.tblWorkOrder.cboBUNIT AS UNIT, dbo.tblWorkOrder_Labor.intLABOR_CODE AS PAYTYPE, dbo.tblWorkOrder_Labor.txtHOURS AS HOURS
FROM         dbo.tblWorkOrder INNER JOIN
                      dbo.tblWorkOrder_Labor ON dbo.tblWorkOrder.txtWORKORDER = dbo.tblWorkOrder_Labor.txtWORKORDER INNER JOIN
                      dbo.tblData_Employee ON dbo.tblWorkOrder_Labor.txtEMPLOYEEID = dbo.tblData_Employee.txtEMPLOYEEID) X
WHERE X.YEAR = @YEAR
AND X.MONTH = @MONTH
END
RETURN @RESULT
END

Open in new window

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question