Pass table column to a user defined function : Function Call « Store Procedure Function « SQL Server / T-SQL






Pass table column to a user defined function


1>
2> CREATE TABLE EmployeeList
3>   (EmployeeName VarChar(100))
4> GO
1> INSERT INTO EmployeeList (EmployeeName) SELECT 'Fred, First'
2> INSERT INTO EmployeeList (EmployeeName) SELECT 'Rubble, Last'
3> GO

(1 rows affected)

(1 rows affected)
1>
2>
3> CREATE FUNCTION fnLastName (@Name VarChar(100))
4>   RETURNS VarChar(100)
5> AS
6>   BEGIN
7>      DECLARE @CommaPosition Int, @LastName varchar(100)
8>      SET @CommaPosition = 6
9>      SET @LastName = SUBSTRING(@Name, 1, @CommaPosition)
10>      RETURN @LastName
11>   END
12> GO
1>
2> SELECT dbo.fnLastName(EmployeeName) As LastName, dbo.fnLastName(EmployeeName) As FirstName
3> FROM EmployeeList
4> GO
LastName                                                                                             FirstName

---------------------------------------------------------------------------------------------------- -------------------
---------------------
Fred,                                                                                                Fred,

Rubble                                                                                               Rubble


(2 rows affected)
1>
2> drop function fnLastName
3> drop table EmployeeList
4> GO
1>
           
       








Related examples in the same category

1.Select from a function returning a table