Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Option (Recompile) in an Inline Table Valued Function

Can OPTION (RECOMPILE) be used in an Inline Table Valued Function?

I try to use it but it gives me error. If it cannot be used what other way is advisable to recompile a Inline Table Valued Function?

My code will be like

ALTER FUNCTION [dbo].[fn_abcwork]
(
  @Date date
  , @id VARCHAR(MAX)
)
RETURNS TABLE
AS
RETURN
  SELECT a.name
    , a.age
    , a.title
    , b.work
  from tbl_abc a
  left outer join tbl_emp on a.id=b.id
like image 464
bl1234 Avatar asked Dec 23 '15 08:12

bl1234


People also ask

What is inline table valued function?

Taking the definition of what a user defined function (UDF) is from Books Online, an inline table-valued function (iTVF) is a table expression that can accept parameters, perform an action and provide as its return value, a table.

What do inline table valued functions return?

A table-valued function returns a single rowset (unlike stored procedures, which can return multiple result shapes). Because the return type of a table-valued function is Table , you can use a table-valued function anywhere in SQL that you can use a table.

What does Option recompile do?

WITH RECOMPILE Option If this option is used when the procedure definition is created, it requires CREATE PROCEDURE permission in the database and ALTER permission on the schema in which the procedure is being created. If this option is used in an EXECUTE statement, it requires EXECUTE permissions on the procedure.

What is the difference between inline table valued function and multi statement?

Inline table valued function refers to a TVF where the function body just contains one line of select statement. There is not return variable. Multi-statement table valued function refers to a TVF where it has a return table variable. Inside the function body, there will be statements populating this table variable.


1 Answers

Inline Table Valued Functions are expanded out into the calling query so nothing is ever actually called. Put the OPTION(RECOMPILE) on the query that is using the function.

like image 97
David Knight Avatar answered Sep 19 '22 14:09

David Knight