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 !




Creating user defined function in sqlserver database


Posted Date: 12 Sep 2008    Resource Type: Articles    Category: Databases

Posted By: sudhajosyula       Member Level: Gold
Rating:     Points: 10



Hi,
Now a days every body like to customize their own related code not only in the front end but also in the back end too.Each is having their own way of designing the database structures.I also personally feel like writing my own functions for the getting the values from the database.I would like to share simply as how to define a user-defined function in sqlserver database.
I am working for my company related to health care domain.In this i am having tblpatientdetails table.In this i am having firstname,lastname and other data fields in it.
Now i want to write a user defined function for retrieving the first name .
The below is my user-defined function for retrieving the first name of the patient.



Use DTNS

create function dbo.GetPatientFirstName()
Returns Table
as
Return select firstname from tblpatientdetails



In order to use it in the front end code you simply use the below query thats all.Your work is done.



Using system.data.sqlClient;

SqlConnection sqlConn=new SqlConnection("You connection");

SqlCommand sqlCmd=new SqlCommand();

SqlDataReader dr;

sqlCmd.Connection=sqlConn;
sqlCmd.CommandText="select * from dbo.GetPatientFirstName()";
sqlConn.Open();
dr=sqlCmd.ExecuteReader();
DropDownList1.DataSource=dr;
DropDownList1.DataTextField="firstname";
DropDownList1.DataTextValue="firstname";
DropDownList1.DataBind();



In this way we can use the user defined functions of our choice.I had given u a simple and easy demo for understanding as how to create and use userdefined function in sqlserver and in our application.

I hope this may be helpful for creating any sort of user defined function easily

Happy Coding !!!!!!!!!!!










Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Simple user defined Function Creation  .  

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: TSQL Datatypes
Previous Resource: Sql Queries
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing services

Contact Us    Privacy Policy    Terms Of Use