Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a temp table in a Table-valued Functions

Tags:

I'm trying to use a temp table in a table-valued function, but it seems I can not. Is there any other way I can use a temp table in a table-valued function?

CURRENT CODE:

CREATE FUNCTION dbo.fnt_AllChildren (@ParentName VARCHAR(255))   RETURNS @return_variable TABLE      (         Id INT,         Name VARCHAR(255),         ParentId INT,         ParentName VARCHAR(255)     ) AS BEGIN     CREATE TABLE #Child (Id INT, Name VARCHAR(255), ParentId INT, ParentName VARCHAR(255))     CREATE TABLE #Parent (Id INT, Name VARCHAR(255), ParentId INT, ParentName VARCHAR(255))       INSERT #Child (Id, Name, ParentId, ParentName)     SELECT child.Id, child.Name, child.ParentId, parent.Name     FROM dbo.t_mytable child     INNER JOIN dbo.t_mytable parent ON child.ParentId = parent.Id     WHERE parent.Name = @ParentName      WHILE (@@ROWCOUNT > 0)     BEGIN         INSERT INTO @return_variable         SELECT * FROM #Child          DELETE FROM#Parent          INSERT INTO #Parent         SELECT * FROM #Child          DELETE FROM #Child                  INSERT INTO #Child (Id, Name, ParentId, ParentName)         SELECT child.Id, child.Name, child.ParentId, parent.Name         FROM dbo.t_mytable child         INNER JOIN #Parent parent ON child.ParentId = parent.Id     END     RETURN END GO 
like image 853
norlando Avatar asked Nov 02 '11 13:11

norlando


People also ask

Can you use temp tables in table valued functions?

Local and global temporary tables play a vital role in the SQL Server scripting. We generally use it to store temporary values for further manipulation. But unfortunately, you cannot use it inside the user defined function.

Can we use CTE in table valued function?

In addition, it is also perfectly valid to use Common Table Expression (CTE) in ITVF. ); Note that ITVF cannot have BEGIN and END block encapsulating the RETURN statement. You will get error “Incorrect syntax near BEGIN” when use BEGIN and END statement as follow.

Can we use temp table in CTE?

This biggest difference is that a CTE can only be used in the current query scope whereas a temporary table or table variable can exist for the entire duration of the session allowing you to perform many different DML operations against them.

Can you pass a temp table to a function in SQL Server?

A TEMP Table of User Defined Table Type has to be created of the same schema as that of the Table Valued parameter and then it is passed as Parameter to the Stored Procedure in SQL Server.


2 Answers

You can use a table variable instead.

DECLARE @Child TABLE (Id INT, Name VARCHAR(255), ParentId INT, ParentName VARCHAR(255)) DECLARE @Parent TABLE (Id INT, Name VARCHAR(255), ParentId INT, ParentName VARCHAR(255)) 
like image 86
Mikael Eriksson Avatar answered Sep 20 '22 20:09

Mikael Eriksson


No.

You can use @table_variables though. Although from a quick glance maybe a recursive CTE might work for you rather than using these child/parent tables at all.

like image 28
Martin Smith Avatar answered Sep 20 '22 20:09

Martin Smith