Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Potentially delusional co-worker, inline vs. multi statement TVF


Potentially delusional co-worker, inline vs. multi statement TVF

Author
Message
sqlnyc
sqlnyc
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 865
Greetings --

I am having a disagreement with a co-worker about what constitutes a multi-statement Table Valued Function.

A sample call to the function would be something like:

SELECT table1.col1, table1.id, othercol1, othercol2
FROM table1 OUTER APPLY (SELECT * FROM func1(id))

I've stripped out the bulk of the function, and included it below.

Co-worker contends that since there only a single SELECT statement that ultimately returns a type of table, and also because he doesn't specifically define the columns/data types that this must be considered "inline".

Certainly doesn't seem that way to me, and so I thought I'd ask the experts at SqlServerCentral.

I have other questions about this example, but I'll include them in another post.

Thanks for any feedback.

SQLNYC

------------------------------

CREATE FUNCTION [dbo].[func1]
(
@param_x INT
)
RETURNS TABLE
AS
RETURN
(
WITH ReminderEntity ( param1, param2 ) AS
(
SELECT ...
),
ReminderParent ( param_y ) AS
(
SELECT TOP 1 some_col FROM
(
SELECT ...
WHERE

UNION ALL

SELECT ...
OUTER APPLY ( SELECT * FROM func2(param1, param2, param3)) AS some_alias
WHERE some_condition
) AS Temp
),
ReminderParentEntity ( param1, param2 ) AS
(
SELECT ...
),
ReminderParentGuidEntity ( param1, param2 ) AS
(
SELECT ...
)
SELECT TOP 1 EmpID FROM
(
SELECT TOP 1 EmpID
FROM
(
SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN

UNION ALL

SELECT ...
INNER JOIN
) AS RemindersEmployee

UNION ALL

SELECT TOP 1 ...
FROM some_table
INNER JOIN
INNER JOIN
LEFT JOIN
LEFT JOIN
CROSS JOIN
WHERE some_condition
) AS Temp
)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24179 Visits: 37948
The skeleton you show us is an inline table valued function.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45036 Visits: 39893
Lynn Pettis (9/3/2012)
The skeleton you show us is an inline table valued function.


Agreed. The table is not explicitly defined and there is no BEGIN/END pair (just to name a couple of the more obvious giveaways on this one).

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
sqlnyc
sqlnyc
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 865
Thanks very much Jeff and Lynn.

Best,

SQLNYC
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47212 Visits: 44368
A multi-statement table valued user-defined function looks like this: (example from Books Online)

CREATE FUNCTION dbo.FindReports (@InEmpID INTEGER)
RETURNS @retFindReports TABLE
(
EmployeeID int primary key NOT NULL,
FirstName nvarchar(255) NOT NULL,
LastName nvarchar(255) NOT NULL,
JobTitle nvarchar(50) NOT NULL,
RecursionLevel int NOT NULL
)
AS
BEGIN
INSERT INTO @retFindReports
SELECT EmployeeID, FirstName, LastName, JobTitle, RecursionLevel
FROM ....


RETURN
END;



The key things being the definition of the table variable and one or more inserts into that table variable


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


sqlnyc
sqlnyc
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 865
Hi Gail and Joe - thanks so much for your replies.

Just wanted to be clear that this is not my code --

Joe - I completely agree that this database is a disaster. An inherited structure that can not easily be changed, unfortunately.

SQLNYC
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search