使用Sql Server 2008返回表值函数返回表结构,就像我的实际表一样?

时间:2017-11-10 06:29:23

标签: sql sql-server sql-server-2008

我有一个名为tableincentive的表,如下面的

create table tableincentive (entry_date date,ag_id int,us_id int,lo_id int,loin_id int,de_id int,dest_id int,incentive_amt decimal(18,2),le_id int,lion_id int,etc...)

我有一个名为[dbo]的表值函数。[func_getdetails]

 Create FUNCTION [dbo].[func_getdetails]
    (
        -- Add the parameters for the function here
        @ason_date date
    )
    RETURNS 
    @tableincentive TABLE 
    (
        -- Add the column definitions for the TABLE variable here
        entry_date date,ag_id int,us_id int,lo_id int,loin_id int,de_id int,dest_id int,incentive_amt decimal(18,2),le_id int,lion_id int,etc...
    )
    AS
    BEGIN
    -- some long process then
    insert into @tableincentive 
    select * from tableincentive;
    end

我的问题是表tableincentive有43列,所以我创建一个表值函数返回tableincentive结构。我需要像我的实际表一样返回。 这意味着在功能

     RETURNS 
            @tableincentive TABLE 
            (
                -- Add the column definitions for the TABLE variable here
                entry_date date,ag_id int,us_id int,lo_id int,loin_id int,de_id int,dest_id int,incentive_amt decimal(18,2),le_id int,lion_id int,etc...
            )
            AS
begin end

更改为

RETURNS  tableincentive
            AS
begin 
end

  RETURNS  @tableincentive table like tableincentive
                AS
    begin 
    end

1 个答案:

答案 0 :(得分:1)

用户定义的函数需要静态结果。这意味着您必须在创建函数时定义返回类型。因此,如果您想要一个静态结果集,那么您可能应该选择存储过程。