Professional Documents
Culture Documents
In SQL server 2000, some times we are supposed to use temporary tables and cursors to
get the required output.
Table1
Department DepartmentName
0001 Depa1
0002 Depa2
0003 Depa3
0004 Depa4
Table2
Employee Department
1 0002
2 0003
3 0001
4 0002
5 0003
If I want to get the Employee’s Department wise, ie, Department Name and the
Employee’s list here Employee’s should be seperated by comma’s
Result
DepartmentName Emp_List
Depa1 3
Depa2 1,4
Depa3 2,5
Depa4
In this case, we have to use the temporary table and then cursor which traverses thru the
each and every department and finds the employee list who belongs to that department
and inserts into the temporary table.
Here we can avoid the temporary table and also the Cursor, by using the User Defined
Functions (SQL Server 2000)
We can create the function which returns the Employee’s list who belongs to a particular
department
Create Function fx_Emp_List (@Department Varchar(5))
RETURNS Varchar(2000) – Employee’s list
As
Begin
Declare @Emp_List Varchar(2000)
RETURN @Emp_List
End