0% found this document useful (0 votes)
11 views

Functions SQL Server

User defined functions in SQL Server allow returning values or datasets. Scalar value functions return a single value after processing, such as fetching a single column from a database. Inline table valued functions return data in the form of a table from a single query.

Uploaded by

Abhishek Roy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Functions SQL Server

User defined functions in SQL Server allow returning values or datasets. Scalar value functions return a single value after processing, such as fetching a single column from a database. Inline table valued functions return data in the form of a table from a single query.

Uploaded by

Abhishek Roy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

User defined functions: User defined functions are useful in case of readymade code being executed within SQL

Server or with C#. These functions are almost similar to the user defined procedures except for a difference that it returns a value or it can return a dataset. Without return clause, we cannot create a function within SQL Server. SQL Server functions are stored in the compiled format and are categorized into the following: 1. Scalar Value Functions: These functions are used to generate a single value after some processing. They may fetch a single column value from the database. e.g. create function myfuncgetemployeebyempno ( @vempno numeric(10) ) Returns varchar(20) As Begin Declare @vename varchar(20) Select @vename=Ename from emp where Empno=@vempno Return @vename End 2. Inline Table Valued Functions: These functions are created where we have a single query returning data in the form of a table. It is recommended that we use these functions where less of logic is required and only selection is going to work. SQL Server supports a table data-type that can receive values from a select query. It is exactly a copy of a table object that gets stored in SQL Server.

Q: How to invoke a scalar value function within C#? A scalar value function can be invoked in C# primarily through a method called execute scalar. A function can be invoked almost in the similar manner as procedures are invoked. SqlConnection vConn=new SqlConnection(server=localhost; database=lovely; user id=abhishek; password=scs); vConn.open(); String vQuery=Select dbo.MyFuncGetEmployeeNameByEmpno(@vEmpno); SqlCommand vComm=new SqlCommand(vQuery, vConn);

vComm.Parameters.AddWithValue(@vEmpno,Convert.ToInt32(textBox1.Text); object t=vComm.ExecuteScalar(); label1.Text=t.ToString(); vConn.Close(); Q: How to call an inline table valued function? Calling an inline table valued function requires a concept of datasets being used for handling of data. We need to have a control that can display multiple records in one go. Dataset on grid-view can be used to represent tabular form data returned by a SQL Server function.

You might also like