简体   繁体   English

在表创建期间调用UDF

[英]Calling a UDF during table creation

I have a UDF that calculates a score based on a range of values that are going in to the table. 我有一个UDF,它会根据进入表中的一系列值来计算分数。

The UDF has to be applied/called during the table creation process, this is where I'm having a little trouble. 在表创建过程中必须应用/调用UDF,这给我带来了一些麻烦。

The UDF had to be created using case when methods only, so I can't change much on that but I'm sure I've just got something a little off. UDF必须仅在使用case方法的情况下创建,因此我不能对此进行太多更改,但是我敢肯定我会有所收获。

I'm not sure if the answer is already out there or not, but I haven;t stumbled across it yet so apologies if this is something already answered. 我不确定答案是否已经存在,但是我还没有偶然发现它,所以如果这已经得到答案,请您道歉。

Here is the UDF which is created first 这是首先创建的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
;

Now here's what I've got for the table creation process 现在这是表创建过程的全部内容

-- 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

The UDF takes the member's info and then calculates their 'cupid' score from that, which is then inserted along with everything else in to the table. UDF获取成员的信息,然后根据该信息计算其“丘比特”得分,然后将其与其他所有信息一起插入表中。

Any help using the UDF would be great 使用UDF的任何帮助都会很棒

尝试将其用于丘比特列:

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

I would very seriously suggest moving the calling of the UDF to the insert/update instead of the table definition. 我会非常认真地建议将UDF的调用移至插入/更新而不是表定义。 To make this easier you could make a simple procedure to insert/update that calls the UDF. 为了使此操作更容易,您可以制作一个简单的过程来插入/更新调用UDF的过程。 That way you avoid having to give the data twice. 这样,您就不必两次提供数据。

here is a very generic example, in this example it looks a bit weird to do all the extra steps for something so simple but that quickly changes when you apply actual scenarios. 这是一个非常普通的示例,在此示例中,对于如此简单但在应用实际方案时会迅速改变的步骤执行所有额外步骤显得有些奇怪。

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

I'm assuming you're at VUW, and I'm assuming you're doing 341 like myself. 我假设您在VUW,并且假设您像我一样在做341。 Use a trigger to call your UDF -I've given you enough to get started on it, you will need to declare and set whatever variables you deem necessary for it to work 使用触发器调用您的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

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

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