Solved

Default parameter in SQL function

Posted on 2008-06-17
3
1,273 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
Comment Utility
CREATE FUNCTION dbo.TESTFUNCTION(@PERIOD INT, @YEAR INT, @MONTH INT = 0)
0
 

Author Comment

by:AaronGreene1906
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now