Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function - help 2

Status
Not open for further replies.

sparkbyte

Technical User
Joined
Sep 20, 2002
Messages
879
Location
US
I have a function that returns a table-value.

What i need to do is account for the user not putting in any values and setting defaults.

There are three parameter values that are passed to the function.

Starting Date
Ending Date
Employee Last Name


If the starting date is not supplied I would like it to default to the beginning of the current day.

if Ending Date is not supplied it should default to the close of the current day.

If the employee name is not given it should default to all employees.

Here is the code for the function.
Code:
USE [MailroomTracking]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_QC_Employee_Report]    Script Date: 06/23/2010 12:34:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		John Fuhrman
-- Create date: 2010/06/22
-- Description:	QC Report
-- =============================================
ALTER FUNCTION [dbo].[fn_QC_Employee_Report] 
(	
	-- Add the parameters for the function here
	@Enter_Starting_Date DateTime, 
	@Enter_Ending_Date DateTime,
	@Enter_Employee_Last_Name VarChar(50)
)
RETURNS TABLE 
AS
RETURN 
(
	-- Add the SELECT statement with parameter references here
SELECT     TOP (100) PERCENT 
		dbo.tblTrackingTable.Tracking_ID, 
		EmployeeFullName = Case
				When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
				When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
				When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
			End,
		dbo.tblEmployee.EmployeeFN, 
		dbo.tblEmployee.EmployeeLN, 
		dbo.tblTrackingTable.EmployeeID, 
		dbo.tblTrackingTable.MachineName, 
		UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber, 
		UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber, 
		dbo.tblTrackingTable.TrackingDate

FROM         dbo.tblTrackingTable FULL OUTER JOIN
                      dbo.tblEmployee ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID

WHERE     (dbo.tblTrackingTable.TrackingDate 
		BETWEEN DATEADD(dd, DATEDIFF(dd, 0, @Enter_Starting_Date), 0) 
		AND DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, @Enter_Ending_Date), 1)))
		AND (dbo.tblEmployee.EmployeeLN Like '%' + @Enter_Employee_Last_Name + '%')
		AND (dbo.tblTrackingTable.EmployeeID IS NOT NULL) 
		AND (dbo.tblTrackingTable.FileNumber <> '') 
		AND (dbo.tblTrackingTable.BoxNumber <> '') 
		AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.') 
		AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)

ORDER BY dbo.tblTrackingTable.TrackingDate, BoxNumber
)

Thanks

John Fuhrman
 
Thanks Mark, I will play with it some more and do some reading.

Thanks

John Fuhrman
 
I am still looking at Marks solution using COALESCE but, here is the code that is working currently.

Code:
USE [MailroomTracking]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_QC_Employee_Report_v1]    Script Date: 06/25/2010 12:58:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		John Fuhrman
-- Create date: 2010/06/22
-- Description:	QC Report
-- =============================================
ALTER FUNCTION [dbo].[fn_QC_Employee_Report_v1] 
(    
    -- Add the parameters for the function here
    @Enter_Starting_Date DateTime,
    @Enter_Ending_Date DateTime,
    @Enter_Employee_Last_Name VarChar(50)
)
[red]
--- Just change the table structure with the correct field types
RETURNS @retFindReports TABLE 
(
        Tracking_ID int,
        EmployeeFullName varchar(8000),
        EmployeeFN varchar(50),
        EmployeeLN varchar(50),
        EmployeeID varchar(15),
        MachineName varchar(20),
        BoxNumber varchar(50),
        FileNumber varchar(8000),
        TrackingDate datetime
)

AS
 BEGIN
      IF @Enter_Starting_Date IS NULL
         SET @Enter_Starting_Date = DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 0)
 
      IF @Enter_Ending_Date IS NULL
         SET @Enter_Ending_Date =  DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 1))
[/red]
    -- Add the SELECT statement with parameter references here
    INSERT INTO  @retFindReports
    SELECT dbo.tblTrackingTable.Tracking_ID,
           EmployeeFullName = Case
                                 When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
                              End,
           dbo.tblEmployee.EmployeeFN,
           dbo.tblEmployee.EmployeeLN,
           dbo.tblTrackingTable.EmployeeID,
           dbo.tblTrackingTable.MachineName,
           UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber,
           UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber,
           dbo.tblTrackingTable.TrackingDate
    FROM dbo.tblTrackingTable
    FULL JOIN dbo.tblEmployee ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID
    WHERE (dbo.tblTrackingTable.TrackingDate BETWEEN DATEADD(dd, DATEDIFF(dd, 0, @Enter_Starting_Date), 0)
        AND DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, @Enter_Ending_Date), 1)))
        AND (dbo.tblEmployee.EmployeeLN Like @Enter_Employee_Last_Name + '%')
        AND (dbo.tblTrackingTable.EmployeeID IS NOT NULL)
        AND (dbo.tblTrackingTable.FileNumber <> '')
        AND (dbo.tblTrackingTable.BoxNumber <> '')
        AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.')
        AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)
   ORDER BY dbo.tblTrackingTable.TrackingDate, BoxNumber
   RETURN

 END
[code]

Thanks

John Fuhrman
 
Here is the inline function:
Code:
GO
/****** Object:  UserDefinedFunction [dbo].[fn_QC_Employee_Report_v1]    Script Date: 06/25/2010 12:58:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        John Fuhrman
-- Create date: 2010/06/22
-- Description:    QC Report
-- =============================================
ALTER FUNCTION [dbo].[fn_QC_Employee_Report_v1] 
(    

    @Enter_Starting_Date DateTime,
    @Enter_Ending_Date DateTime,
    @Enter_Employee_Last_Name VarChar(50)
)

--- Just change the table structure with the correct field types
RETURNS TABLE
AS
    RETURN SELECT dbo.tblTrackingTable.Tracking_ID,
           EmployeeFullName = Case
                                 When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
                              End,
           dbo.tblEmployee.EmployeeFN,
           dbo.tblEmployee.EmployeeLN,
           dbo.tblTrackingTable.EmployeeID,
           dbo.tblTrackingTable.MachineName,
           UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber,
           UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber,
           dbo.tblTrackingTable.TrackingDate
    FROM dbo.tblTrackingTable
    FULL JOIN dbo.tblEmployee ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID
    WHERE (dbo.tblTrackingTable.TrackingDate BETWEEN DATEADD(dd, DATEDIFF(dd, 0, COALESCE(@Enter_Starting_Date,DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 0))), 0)
        AND DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, COALESCE(@Enter_Ending_Date,DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 1)))), 1)))
        AND (dbo.tblEmployee.EmployeeLN Like @Enter_Employee_Last_Name + '%')
        AND (dbo.tblTrackingTable.EmployeeID IS NOT NULL)
        AND (dbo.tblTrackingTable.FileNumber <> '')
        AND (dbo.tblTrackingTable.BoxNumber <> '')
        AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.')
        AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)
  -- ORDER BY dbo.tblTrackingTable.TrackingDate, BoxNumber

PluralSight Learning Library
 
This is interesting. (expand out until columns line up)

Client stats.

Code:
Version 1 Query

                                                           Trial  4              Trial  3      Trial  2    Trial  1      Average
  Client Execution Time                                    14:33:38              14:33:35      14:31:47    14:30:57
  Query Profile Statistics
    Number of INSERT, DELETE and UPDATE statements            0                     0            0            0           0.0000
    Rows affected by INSERT, DELETE, or UPDATE statem...      0                     0            0            0           0.0000
    Number of SELECT statements                               5                     0            0            0           1.2500
    Rows returned by SELECT statements                        3229                  0            0            0           807.2500
    Number of transactions                                    0                     0            0            0           0.0000
  Network Statistics
    Number of server roundtrips                               3                     3            3            3           3.0000
    TDS packets sent from client                              4                     4            4            3           3.7500
    TDS packets received from server                          124                   3            3            3           33.2500
    Bytes sent from client                                    4416                  4396         4460         1716        3747.0000
    Bytes received from server                                498620                63           202          228         124778.3000
  Time Statistics
    Client processing time                                    31                    0            0            15          11.5000
    Total execution time                                      1515                  15           15           46          397.7500
    Wait time on server replies                               1484                  15           15           31          386.2500



Version 2 Query
                                                             Trial  4          Trial  3          Trial  2         Trial  1          Average
 Client Execution Time                                       14:41:00          14:34:47          14:34:38          14:34:18
 Query Profile Statistics
   Number of INSERT, DELETE and UPDATE statements            0                 0                 0                 0                0.0000
   Rows affected by INSERT, DELETE, or UPDATE statem...      0                 0                 0                 0                0.0000
   Number of SELECT statements                               5                 5                 5                 5                5.0000
   Rows returned by SELECT statements                        3394              3262              3259              3242             3289.2500
   Number of transactions                                    0                 0                 0                 0                0.0000
 Network Statistics
   Number of server roundtrips                               3                 3                 3                 3                3.0000
   TDS packets sent from client                              3                 3                 3                 3                3.0000
   TDS packets received from server                          130               125               125               125              126.2500
   Bytes sent from client                                    3870              3870              3870              3870             3870.0000
   Bytes received from server                                521565            503170            502752            500385           506968.0000
 Time Statistics
   Client processing time                                    47                47                31                46               42.7500
   Total execution time                                      7015              7108              7202              6967             7073.0000
   Wait time on server replies                               6968              7061              7171              6921             7030.2500

I find it interesting that the execution time between the 2 queries is vastly different and the byte recieved a by a magnatude different even though the record set returned is the same.

The script using the COALESCE takes 42.75 average processing time and the other averages 11.5.

For reference here are the 2 scripts (out of the functions)

Code:
USE [MailroomTracking]

Declare
    @Enter_Starting_Date DateTime,
    @Enter_Ending_Date DateTime,
    @Enter_Employee_Last_Name VarChar(50)

Set @Enter_Starting_Date = '06/25/2010'
Set @Enter_Ending_Date = '06/25/2010'
Set @Enter_Employee_Last_Name = 'dixon'

      IF @Enter_Starting_Date IS NULL
         SET @Enter_Starting_Date = DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 0)
 
      IF @Enter_Ending_Date IS NULL
         SET @Enter_Ending_Date =  DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 1))

    -- Add the SELECT statement with parameter references here

    SELECT dbo.tblTrackingTable.Tracking_ID,
           EmployeeFullName = Case
                                 When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
                              End,
           dbo.tblEmployee.EmployeeFN,
           dbo.tblEmployee.EmployeeLN,
           dbo.tblTrackingTable.EmployeeID,
           dbo.tblTrackingTable.MachineName,
           UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber,
           UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber,
           dbo.tblTrackingTable.TrackingDate
    FROM dbo.tblTrackingTable
    FULL JOIN dbo.tblEmployee ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID
    WHERE (dbo.tblTrackingTable.TrackingDate BETWEEN DATEADD(dd, DATEDIFF(dd, 0, @Enter_Starting_Date), 0)
        AND DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, @Enter_Ending_Date), 1)))
        AND (dbo.tblEmployee.EmployeeLN Like @Enter_Employee_Last_Name + '%')
        AND (dbo.tblTrackingTable.EmployeeID IS NOT NULL)
        AND (dbo.tblTrackingTable.FileNumber <> '')
        AND (dbo.tblTrackingTable.BoxNumber <> '')
        AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.')
        AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)
   ORDER BY dbo.tblTrackingTable.TrackingDate, BoxNumber
Code:
Declare
    @Enter_Starting_Date DateTime,
    @Enter_Ending_Date DateTime,
    @Enter_Employee_Last_Name VarChar(50)

Set @Enter_Starting_Date = '06/25/2010'
Set @Enter_Ending_Date = '06/25/2010'
Set @Enter_Employee_Last_Name = 'dixon'


SELECT dbo.tblTrackingTable.Tracking_ID,
           EmployeeFullName = Case
                                 When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
                                 When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
                              End,
           dbo.tblEmployee.EmployeeFN,
           dbo.tblEmployee.EmployeeLN,
           dbo.tblTrackingTable.EmployeeID,
           dbo.tblTrackingTable.MachineName,
           UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber,
           UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber,
           dbo.tblTrackingTable.TrackingDate
    FROM dbo.tblTrackingTable
    FULL JOIN dbo.tblEmployee 
		ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID
    
WHERE 
	(dbo.tblTrackingTable.TrackingDate 
		BETWEEN DATEADD(dd, DATEDIFF(dd, 0, COALESCE(@Enter_Starting_Date,DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 0))), 0)
        AND DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, COALESCE(@Enter_Ending_Date,DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, CURRENT_TIMESTAMP), 1)))), 1)))
        
		AND (dbo.tblEmployee.EmployeeLN Like @Enter_Employee_Last_Name + '%')
        AND (dbo.tblTrackingTable.EmployeeID IS NOT NULL)
        AND (dbo.tblTrackingTable.FileNumber <> '')
        AND (dbo.tblTrackingTable.BoxNumber <> '')
        AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.')
        AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)


Thanks

John Fuhrman
 
I am sorry, but I wanted to ask one more question.

Althought the 2 functions work well. Both reurn empty sets if the user does not specify a Employee to report on where it should return all Employees.

This Stored Procedure seems to work better than both the functions and is based on Version 1 of the function.

Code:
USE [MailroomTracking]
GO
/****** Object:  StoredProcedure [dbo].[usp_QC_Employee_Report]    Script Date: 06/25/2010 15:10:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		John Fuhrman
-- Create date: 2010/06/22
-- Description:	QC Report
-- =============================================

ALTER PROCEDURE [dbo].[usp_QC_Employee_Report] 
	-- Add the parameters for the stored procedure here

	@Enter_Starting_Date DateTime, 
	@Enter_Ending_Date DateTime,
	@Enter_Employee_Last_Name VarChar(50)

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	IF 	IsDate(@Enter_Starting_Date) <> 1 
		 Set @Enter_Starting_Date = Convert(varchar,Getdate(),101)

	IF 	IsDate(@Enter_Ending_Date) <> 1 
		Set @Enter_Ending_Date = Convert(varchar,Getdate(),101)

	IF @Enter_Employee_Last_Name IS NULL
		SET @Enter_Employee_Last_Name =  '%'

SELECT     TOP (100) PERCENT 
		dbo.tblTrackingTable.Tracking_ID, 
		EmployeeFullName = Case
				When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
				When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
				When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
			End,
		dbo.tblEmployee.EmployeeFN, 
		dbo.tblEmployee.EmployeeLN, 
		dbo.tblTrackingTable.EmployeeID, 
		dbo.tblTrackingTable.MachineName, 
		UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber, 
		UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber, 
		dbo.tblTrackingTable.TrackingDate

FROM         dbo.tblTrackingTable FULL OUTER JOIN
                      dbo.tblEmployee ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID

WHERE     
(dbo.tblTrackingTable.TrackingDate BETWEEN 
	DATEADD(dd, DATEDIFF(dd, 0, @Enter_Starting_Date), 0) 
	And DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, @Enter_Ending_Date), 1)))
		AND	(dbo.tblEmployee.EmployeeLN Like @Enter_Employee_Last_Name + '%')
		AND	(dbo.tblTrackingTable.EmployeeID IS NOT NULL) 
		AND (dbo.tblTrackingTable.FileNumber <> '') 
		AND (dbo.tblTrackingTable.BoxNumber <> '') 
		AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.') 
		AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)

ORDER BY dbo.tblTrackingTable.TrackingDate, BoxNumber

END

What I did find interesting though is the IF for the EmployeeLN. The [red] '%'[/red] is needed to return all the Employees data.


Thanks

John Fuhrman
 
I think you're comparing different scenarios. In the plain code Boris version is going to win over coalesce. However, if we compare multi-line function performance with inline function performance, the inline function should win.

Can you compare performance by using two functions,

say,

select * from dbo.MultiLine

vs.

select * from dbo.Inline order by MyOrder


PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top