There are two types of functions. Table-valued and Scalar functions. A table valued function can return a table of data (multiple rows and multiple columns). A scalar function can only return a single value. If you make a scalar function, you can call it from within the select clause. For example...
Code:
Create Function [dbo].[dd_GUID_Description] (@GUID_Value varchar(36))
RETURNS [!]VarChar(100)[/!]
AS
Begin
RETURN (
SELECT dbo.Drop_Downs.Description
FROM drop_downs
WHERE DropDown_Guid =@GUID_Value
)
End
Note that I used varchar(100) as the return type. This should match the data type of the description column. Since this is a scalar function, you can use it in the select clause like this:
Code:
Select Column1, dbo.dd_GUID_Description(Column2) as New_Column2
From Table1
There are a couple other comments I would like to make about this. First, there is a data type specifically for guids called UniqueIdentifier. When you store a GUID as a string, it takes 36 bytes when you include the dashes or 32 bytes if you remove them. Given the nature of GUID's (each character can be 0 through F), you can actually store the data in 16 bytes by compressing/combining the data. SQL Server does this automatically when you use the UniqueIdentifier data type. Storage space is cheap, so size doesn't matter too much. However, when you join/filter using GUIDs, it's better to use a 16 byte value instead of a 36 byte string. With millions of rows, you will notice a performance improvement by using UniqueIdentifier.
My next comment also involves performance. When you use a function, there is an inherent overhead associated with it. Each call to a function is pretty fast, but depending on how you use the function, it can cause real performance problems for you. My (personal) criteria for a function is two-fold. It must satisfy both conditions before I bother creating a function. 1. The function must be sufficiently complicated that I want to hide the implementation details. 2. The function must be re-usable by several queries. If the block of code does not satisfy both criteria, I will not make it a function.
From the sounds of it, you don't really need a function here. A simple join to the table should suffice. It also sounds like you may want the same join on several columns from the same master table. In that case, you should be able to accomplish this using table & column aliases. Like this...
Code:
Select Table1.Column1,
Alias1.Description As Alias1Description,
Alias2.Description As Alias2Description
From Table1
Inner Join Drop_Downs As Alias1
On Table1.Column2 = Alias1.DropDown_Guid
Inner Join Drop_Downs As Alias2
On Table1.Column3 = Alias2.DropDown_Guid
Multiple joins will be many times faster than calling a function for each row.
-George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom