using Functions

Posted on 09/03/2011. Filed under: Sql Server, Sqlserver2008 |

/*This fuction we use it almost with lookup table to return the name it takes two parameters first one display the table and the second one  display the id that we want to return its related name*/
 

USE 
 

[databaseName]

GO
 

SET
 

/****** Object: UserDefinedFunction [dbo].[GetName] **/

 ANSI_NULLS

ONGO
 

SET
 QUOTED_IDENTIFIER
 
 ALTER function [dbo].[GetName](@Flag int, @ID int)

Returns
varchar(120)Begin
 
 

DECLARE
 @Name varchar(120
 – 1:Site 2:JobLevel 3:Role
 IF 

END
ELSE IF @Flag=2 BEGIN select @Name=JobLevelDesc from dbo.JobLevel where JobLevelID=@ID  

END
 
ELSE IF @Flag=3 BEGIN select @Name=RoleDesc from dbo.UserRoles where RoleID =@ID

 @Flag=1 BEGIN select @Name=SiteDesc from dbo.Site where SiteID=@ID

)ONGO

 END
 Return
 @Name
END
Advertisement

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.