create function select_salary ()
returns table
as
return
(
select * from staff where salary>1000
)
这样的函数可以用;
但是当加上begin和end后就不可以了。为什么呢?
create function select_salary ()
returns table
as
begin
return
(
select * from staff where salary>1000
)
end

解决方案 »

  1.   

    内嵌表值函数CREATE FUNCTION [ owner_name.] function_name
        ( [ { @parameter_name [AS] scalar_parameter_data_type [ = default ] } [ ,...n ] ] ) RETURNS TABLE [ WITH < function_option > [ [,] ...n ] ] [ AS ]RETURN [ ( ] select-stmt [ ) ]
    ---
    这是标准内嵌表值函数写法,加了begin就出错哦
      

  2.   

    我看到的是这个:
    CREATE FUNCTION <Inline_Function_Name, sysname, FunctionName> 
    (
    -- Add the parameters for the function here
    <@param1, sysname, @p1> <Data_Type_For_Param1, , int>, 
    <@param2, sysname, @p2> <Data_Type_For_Param2, , char>
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
    -- Add the SELECT statement with parameter references here
    SELECT 0
    )
    GO
      

  3.   


    意义当然大。当然静态select的意义或许不大,简单的select意义意义或许不大。我们往往将复杂的结果集创建成视图,以方便使用,但视图不支持参数查询,而内嵌表函数就可以支持参数查询,这就是意义所在。