访问从SP返回表值函数的表

时间:2018-01-22 07:38:36

标签: sql sql-server tsql stored-procedures stored-functions

我有表值函数

ALTER FUNCTION [dbo].[fn_Functiont]()
RETURNS TABLE 
AS
RETURN 
(   
SELECT d.*, b.Name AS Name, ps.Name AS PaymentSystemName, c.UserName AS UserName, c.FirstName AS ClientFirstName, c.LastName AS LastName, c.Number AS DocumentNumber, c.Id
FROM Document AS d
JOIN System AS ps ON d.SystemId = ps.Id
JOIN Client AS c ON c.Id = d.ClientId
LEFT JOIN Shop AS b ON b.Id = d.ShopId
WHERE d.OperationTypeId IN (2, 4, 5) AND c.Type = 1
)

和SP。在那个SP我已经宣布了像这样的临时表

DECLARE @tempTable AS TABLE 
(
   .. columns here ...
)

宣布我只是插入信息

INSERT INTO @tempTable
SELECT * FROM [dbo].[fn_Functiont]()
Select @column1,colum2...,from @tempTable

问题是我必须在@temptable中声明很多列,代码看起来很难看。那么有更好的方法从表值函数中读取SP中的行吗?

1 个答案:

答案 0 :(得分:1)

而不是表变量@tempTable使用临时表并尝试此

SELECT * INTO #tempTable FROM [dbo].[fn_Functiont]()