Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

calling multiple sql udf's from a stored proc to look up data

$
0
0

I have a sp that needs to show some lookup values from another table that stores custom field labels and values.

I wrote a couple table valued inline udf's as below:
This one retuns a custom value based on a passed label and FileKey

   

USE [CorpInfo]
    GO
    /****** Object:  UserDefinedFunction [dbo].[GetCustomFieldValue]    Script Date:   08/09/2013 12:52:35 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER FUNCTION [dbo].[GetCustomFieldValue] 
    ( 
 @FieldName nvarchar(50),
 @FileKey int
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
 SELECT     view_Attendance_EmployeeFields.ASSTRING
    FROM         view_Attendance_CustomFields LEFT OUTER JOIN
                      view_Attendance_EmployeeFields ON     view_Attendance_CustomFields.UNIQUEID = view_Attendance_EmployeeFields.FIELDID
    WHERE     (view_Attendance_CustomFields.NAME = @FieldName) AND (view_Attendance_EmployeeFields.FileKey = @FileKey)
    )


This one gets an employee photo:

   
USE [CorpInfo]
    GO
    /****** Object:  UserDefinedFunction [dbo].[GetEmpPhoto]    Script Date: 08/09/2013     12:55:05 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER FUNCTION [dbo].[GetEmpPhoto]
    ( 
 @FileKey int
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
 SELECT     view_Attendance_EmpPhoto.UserData
 FROM         view_Attendance_EmpPhoto 
 WHERE     view_Attendance_EmpPhoto.FileKey = @FileKey
    )

Here is the sp that uses these udf's

    ALTER PROCEDURE [dbo].[GetDirList_CorpDirIOHR]
 -- Add the parameters for the stored procedure here
 @Lastname as varchar(10)  = null,
 @Firstname as varchar(10) =null ,
 @Facility as integer,
 @Department as integer 
    AS
    BEGIN
 begin
 if @Lastname is null
 set @Lastname = ''
 if @Firstname is null
 set @Firstname =''
 end


    IF @Facility  =0 AND @Department = 0


    SELECT     view_Attendance_Employees.FileKey, 
     Case when Select ASSTRING FROM   GetCustomFieldValue('Preferred First Name', view_Attendance_Employees.FileKey) = '' 
      then view_Attendance_Employees.FirstName 
         else Select ASSTRING FROM  GetCustomFieldValue('Preferred First Name', view_Attendance_Employees.FileKey) 
     end as FirstName
       
       + ' ' + view_Attendance_Employees.LastName AS  Name, 
     OrganizationalUnit.OrgUnitName, 
     view_Attendance__WORKGROUP1.NAME AS Facility, 
        CASE WHEN Select ASSTRING FROM  GetCustomFieldValue('Plascore Desk Phone', view_Attendance_Employees.FileKey) = '' 
      OR Select ASSTRING FROM GetCustomFieldValue('Plascore Desk Phone',view_Attendance_Employees.FileKey) = '0' 
      THEN ' ' ELSE 
      Select ASSTRING FROM GetCustomFieldValue('Plascore Desk Phone',view_Attendance_Employees.FileKey) 
     END AS Ext, 
        CASE WHEN Select ASSTRING FROM GetCustomFieldValue('Plascore Cell Phone',view_Attendance_Employees.FileKey) = '' 
     OR Select ASSTRING FROM GetCustomFieldValue('Plascore Cell Phone',view_Attendance_Employees.FileKey) = '0' 
     THEN ' ' 
     ELSE '(' + LEFT(Select ASSTRING FROM GetCustomFieldValue('Plascore Cell Phone',view_Attendance_Employees.FileKey), 3) + ') ' + 
       LEFT(RIGHT(Select ASSTRING FROM GetCustomFieldValue('Plascore Cell Phone',view_Attendance_Employees.FileKey), 7), 3) + ' - ' + RIGHT(Select ASSTRING  FROM GetCustomFieldValue('Plascore Cell Phone',view_Attendance_Employees.FileKey), 4) 
     END AS Cell, 
     Select ASSTRING FROM GetCustomFieldValue('Plascore Email Address',view_Attendance_Employees.FileKey) as Email, 
     Employee.JobTitle,  
     SELECT UserData FROM GetEmpPhoto(view_Attendance_Employees.FileKey,view_Attendance_Employees.FileKey) AS PIC,
                    view_Attendance_Employees.FirstName, 
     view_Attendance_Employees.LastName,
        case when SELECT cnt FROM GetEmpPunchStatus(@File) % 2 = 0 
     then 'OUT'  
     else 'IN' end as EmpINOUT, 
     view_Attendance_Employees.FileKey
    FROM         Employee WITH (nolock)
       INNER JOIN view_Attendance_Workgroup1 WITH (nolock) ON view_Attendance_Employees.WG1 = view_Attendance_Workgroup1.WGNUM  
       LEFT OUTER JOIN view_Attendance_Transactions ON  view_Attendance_Employees.FileKey = view_Attendance_Transactions.FileKey
    WHERE     (view_Attendance_Employees.Status = 1) AND (view_Attendance_Employees.Badge >  0) AND (view_Attendance_Employees.LastName LIKE CASE WHEN ltrim(rtrim(@LastName)) 
                      LIKE '' THEN '%' ELSE '%' + @LastName + '%' END) AND  (view_Attendance_Employees.FirstName LIKE CASE WHEN ltrim(rtrim(@FirstName)) 
                      LIKE '' THEN '%' ELSE '%' + @FirstName + '%' END)
  ORDER BY FirstName,Facility

So basically am I using the right idea to use udf's as "look up" functions, and have I coded the used of them the right way?


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>