[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Default parameter in SQL function

Posted on 2008-06-17
3
Medium Priority
?
1,283 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview

834 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