C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to create User Defined Function in Sql


Posted Date: 06 Jun 2008    Resource Type: Code Snippets    Category: SQL
Author: R O B I NMember Level: Gold    
Rating: Points: 10



The following code sample shows how to create User Defined Function in Sql .


CREATE FUNCTION GETTRAININGSTATUS
(
@USERID VARCHAR(25),
@TRAININGID INT)RETURNS INT
AS
BEGIN
DECLARE @COUNT INT
DECLARE @RETURNVALUE INT
SET @COUNT = (SELECT COUNT(*) FROM TBLTRAININGS WHERE USERID = @USERID
AND TRAININGID = @TRAININGID)
IF(@COUNT = '')
SET @RETURNVALUE = 0
ELSE IF (@COUNT = 1)
SET @RETURNVALUE = 1
ELSE IF (@COUNT = 0)
SET @RETURNVALUE = 1
RETURN @RETURNVALUE
END


The function will fetch equivalent value for the element in the table. The same can be achieved with the help of cursors, by I wanted to try it with functions hence the code.

The following stored procedure is used for calling the function.


CREATE PROCEDURE SP_TRAININGSTATUS
@USERID varchar(50)
AS
SELECT TRAININGID, TRAINING, dbo.GETTRAININGSTATUS(@USERID, @TRAININGID)
AS SKILLSTATUS
FROM
TBLTRAININGS GO




Responses

Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code is going to help lots of guys.
Ton Thanks to you
Regards,
Kapil



Author: Roshan R Mhatre    24 Jul 2008Member Level: Gold   Points : 1
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code is going to help lots of guys.
Ton Thanks to you
Regards,
Roshan


Author: Nirupa Rani Acharya    27 Jul 2008Member Level: Bronze   Points : 1
really thanks 4 dis code. plez send more db code & share ur intellegence with us.

Thanks & Regards,

Nirupa Rani Acharya,
C.S.Informatics (P) Ltd., Pune



Author: Nirupa Rani Acharya    27 Jul 2008Member Level: Bronze   Points : 1
really thanks 4 dis code. plez send more db code & share ur intellegence with us.

Thanks & Regards,

Nirupa Rani Acharya,
C.S.Informatics (P) Ltd., Pune



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to get date part from a datetime column
Previous Resource: How to format date in MS SQL
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use