Thursday, 31 May 2012

Passing Temporary  Table as parameter to SQL Function

  1. Define your own Table Type
  2. Declare your table Variable of your own table type
  3. Insert the Record
  4. Pass the table variable name as the parameter to function
Define your own Table Type
CREATE TYPE TableType AS TABLE (LocationName VARCHAR(50))
GO 

Declare your table Variable of your own table type 
DECLARE @myTable TableType

Insert the Record
INSERT INTO @myTable(LocationName) VALUES('aaa')
SELECT * FROM @myTable
 
 
Pass the table variable name as the parameter to function 
CREATE FUNCTION Example( @TableName TableType READONLY)
RETURNS VARCHAR(50)
AS
BEGIN
    DECLARE @name VARCHAR(50)

    SELECT TOP 1 @name = LocationName FROM @TableName
    RETURN @nameEND
 
 
For More Reference Click Below Link
http://stackoverflow.com/questions/1609115/pass-table-as-parameter-into-sql-server-udf 
 

No comments:

Post a Comment