在表创建期间调用UDF

时间:2014-09-01 04:42:50

标签: sql sql-server tsql user-defined-functions

我有一个UDF,它根据进入表格的一系列值计算得分。

在表创建过程中必须应用/调用UDF,这是我遇到一点麻烦的地方。

UDF必须使用仅使用方法的情况创建,因此我无法对此进行更改,但我确信我只是稍微有点了。

我不确定答案是否已经存在,但是我还没有发现它,但是如果这已经得到了解答,那么就道歉了。

这是首先创建的UDF

--Create UDF
create function [dbo].[cupidscoreUDF]
(
    @gender char(1),
    @name varchar(15),
    @dob datetime,
    @weight int,
    @height int,
    @smoker bit,
    @salary int
)
returns int
as
begin
    declare @score int

    -- To determine age in years
    declare @Age int 
    select @Age = DATEDIFF(YEAR, @dob, GETDATE())

    select 
        @score = case
                    when @Age between 20 and 30 then 5
                    when @Age between 31 and 40 then 4
                    when @Age between 41 and 50 then 3
                    when @Age > 50 then 2
                    else 0
                end

    -- To determine the height/weight ratio
    declare @WeightHeight int

    set @WeightHeight = @weight / @height 

    set
        @score = @score +
                    case
                        when @WeightHeight between 20 and 25 then 1
                        when @WeightHeight between 25 and 30 then 3
                        when @WeightHeight between 30 and 35 then 4
                        when @WeightHeight between 35 and 40 then 2
                        else 0
                    end

    -- If non-smoker add 2 points
    if @smoker = 0
        set @Score = @Score + 2

    -- To determine score by salary
    set
        @score = @score + 
                    case
                        when @salary < 50000 then 1
                        when @salary between 500001 and 60000 then 2
                        when @salary between 60001 and 70000 then 3
                        when @salary > 70000 then 4
                    end

    return @score
end
;

现在,我在桌面创建过程中获得了

-- Create Member_Profile table
create table Member_Profile
(
MemberID int primary key,
Gender varchar(6),
Name varchar(50),
Dob datetime,
Weight int,
Height int,
Smoker bit,
Salary int,
Cupid as dbo.cupidscoreUDF
)
GO

insert into Member_Profile (Gender, Name, Dob, Weight, Height, Smoker, Salary) values ('Male','James',19931115, 75, 180, 0, 80000);
insert into Member_Profile (Gender, Name, Dob, Weight, Height, Smoker, Salary) values ('Female','Rosie',19870912, 45, 150, 0, 100000);
insert into Member_Profile (Gender, Name, Dob, Weight, Height, Smoker, Salary) values ('Male','Richard',19630402, 95, 168, 1, 200000);

select * from Member_Profile

UDF获取会员的信息,然后计算他们的丘比特&#39;从中得分,然后将其与其他所有内容一起插入到表格中。

使用UDF的任何帮助都很棒

3 个答案:

答案 0 :(得分:1)

尝试将此用于丘比特专栏:

Cupid as dbo.cupidscoreUDF(Gender, Name, Dob, Weight, Height, Smoker, Salary)

答案 1 :(得分:1)

我非常认真地建议将UDF的调用移到insert / update而不是表定义。为了使这更容易,您可以创建一个简单的插入/更新过程来调用UDF。这样你就可以避免两次提供数据。

这是一个非常通用的例子,在这个例子中,为一些如此简单的事情做所有额外步骤看起来有点奇怪,但是当你应用实际场景时会很快改变。

use demo
go

create table dbo.example_table (
id int identity(1,1) primary key,
some_value_1 int,
some_value_2 int,
some_calculation int)

go

create function dbo.calculator_function (
@value1 int,
@value2 int)
returns int
as
begin
declare @result int = (select @value1 + @value2 as result)
return @result
end

go

create procedure dbo.insert_example 
    @value1 int,
    @value2 int
as
insert dbo.example_table (some_value_1,some_value_2,some_calculation) 
select @value1, @value2, dbo.calculator_function(@value1,@value2)

go

exec dbo.insert_example 1,2

go 

select * from example_table

答案 2 :(得分:0)

我假设你在VUW,我假设你像我一样做着341。 使用触发器调用UDF - 我已经给你足够的开始,你需要声明并设置你认为必要的变量才能工作

CREATE TRIGGER [dbo].[CalculateCupid]
 ON [dbo].[Member_Profile]
 AFTER INSERT
AS
BEGIN




UPDATE DBO.Member_Profile 
SET Cupid_Score = @Cupid_Score WHERE MemberID=@MemberID

END