Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Can a multistatement table-valued function return a user-defined table type TABLE? Expand / Collapse
Author
Message
Posted Wednesday, October 23, 2013 10:44 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 25, 2014 5:41 AM
Points: 128, Visits: 820
I've simplified the code as much as possible below. It works but I want to use a "user-defined table type" instead of writing out all the columns in the "returns" table. I don't want to manually maintain the columns returned in table functions so I'd like to use a user-defined table type instead. Can it be done?

----------- make table type dbo.xy
IF EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'xy' AND ss.name = N'dbo')
DROP TYPE dbo.xy
GO

CREATE TYPE dbo.xy AS TABLE
(
x int NOT NULL,
y int NOT NULL
)
GO
----------- make fn DoubleXY
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'dbo.DoubleXY')
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION dbo.DoubleXY
GO

CREATE FUNCTION dbo.DoubleXY (@a int, @b int)
returns @t table (x int, y int)
--next line fails. How can I return @t dbo.xy - a user-defined table type in a multistatement table-valued function?
--returns @t dbo.xy
AS
BEGIN
insert into @t values (@a, @b);
insert into @t values (@a * 2, @b * 2);

RETURN
END
GO

-------------- test
DECLARE @points dbo.xy;

insert into @points
select * from dbo.DoubleXY(5, 7);

insert into @points
select * from dbo.DoubleXY(22, 24);

select * from @points;


Post #1507745
Posted Wednesday, October 23, 2013 12:06 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:11 PM
Points: 15,517, Visits: 27,898
According to the documentation, you have to use a table variable. That means defining the columns.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1507783
Posted Wednesday, October 23, 2013 10:13 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 9:44 PM
Points: 3,610, Visits: 5,223
With a multi-line TVF like you have there, as Grant points out you need to define the table columns.

If you can convert it into an inline TVF (and the sample you provided could be) there would be no need to define the table columns.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1507872
Posted Thursday, October 24, 2013 6:10 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 25, 2014 5:41 AM
Points: 128, Visits: 820
Looking at the history, Microsoft added Tables as a return type in 2005. They added user-defined table types in 2008 so maybe Microsoft never went back extend the grammar. Perhaps it was too much effort. The documentation examples for passing tables as arguments really pushes us to use user-defined table types - that's why this limitation surprises me.

I'm a control freak over parameters. I require developers to use parameter names that match column names; that way I can run automated checks to make sure data types and lengths match before deploying to production.

Passing input, readonly tables is so powerful and object oriented, I love it.
Post #1507995
Posted Thursday, October 24, 2013 6:13 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:11 PM
Points: 15,517, Visits: 27,898
Just don't ever forget that all these types of tables from table variables, multi-statement UDF and table parameters, all of them lack statistics which can seriously, negatively, impact performance.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1507998
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse