简体   繁体   English

如何在存储过程中分配函数返回值?

[英]how to assign a function return value in storedprocedure?

I have a function ufnGetTVP and needs to assign the return value to a table variable in stored procedure. 我有一个函数ufnGetTVP ,需要将返回值分配给存储过程中的表变量。

I have tried the below code but getting error. 我尝试了下面的代码,但出现错误。 I couldnt find any relevant articles to fix this issue. 我找不到任何相关文章来解决此问题。

create proc testfunc( @input varchar(50))
as 
begin
DECLARE @mt TABLE
set  @mt = select * from ufnGetTVP(@input)

end

It will be helpful , if someone could identify what goes wrong here. 如果有人可以识别出这里出了什么问题,这将是有帮助的。

You can use Temp tables also, You don't have to worry about column definitions. 您还可以使用Temp表。您不必担心列定义。

create proc testfunc( @input varchar(50))
as 
begin

    select * into #temp from ufnGetTVP(@input)

    select * from #temp

    IF OBJECT_ID('tempdb..#temp') IS NOT NULL drop table #temp

end

First, you need to include the structure of the table you are declaring: 首先,您需要包括要声明的表的结构:

DECLARE @mt as TABLE
(
    <columns list>
)

Second, you can't use set , you need to use insert : 其次,您不能使用set ,而需要使用insert

INSERT INTO @mt (<columns list>)
SELECT <columns list> FROM dbo.ufnGetTVP(@input)

Have you tried inserting into the variable? 您是否尝试过插入变量?

Also declare the columns on your table to match the function: 还声明表上的列以匹配该函数:

DECLARE @mt TABLE (Column1 INT, Column2 Int...)

INSERT INTO @mt
SELECT Column1, Column2, ... FROM ufnGetTVP(@input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM