Friday, June 09, 2006

Retrieve Column Info Of Table Valued Functions By Using INFORMATION_SCHEMA.ROUTINE_COLUMNS

How do you retrieve the columns of Table Valued Functions?
This question was posted to day in the microsoft.public.sqlserver.programming
forum. Although I answered the question I must admit that I have never used the INFORMATION_SCHEMA.ROUTINE_COLUMNS before

So let's see how it works
First create a User-Defined Functions that returns a table data type


USE pubs
GO



CREATE FUNCTION LargeOrder ( @FreightParm
style="color:#3333ff;">money
)
RETURNS @OrderShipperTab TABLE
(
OrderPrice MONEY,
OrderDate DATETIME
)
AS
BEGIN
INSERT
@OrderShipperTab
SELECT OrderPrice, OrderDate
FROM Orders
WHERE OrderPrice > @FreightParm
RETURN
END


--Let's test the function
-- This will return all orders with an order price greater than $40

SELECT *
FROM LargeOrder( 40 )


--Now let's get the column information

SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE,ORDINAL_POSITION
FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS
WHERE TABLE_NAME ='LargeOrder'
ORDER BY ORDINAL_POSITION

No comments:

Post a Comment