2014-09-01 4 views
0

테이블에 들어가는 값의 범위에 따라 점수를 계산하는 UDF가 있습니다.테이블 작성 중에 UDF 호출

UDF는 테이블 생성 프로세스 중에 적용/호출되어야하는데, 여기가 약간 문제가 있습니다.

UDF는 메서드 만 사용할 때 대/소문자를 사용하여 만들어야했기 때문에 그 부분을 많이 변경할 수는 없지만 조금만 벗어났습니다.

답변이 이미 존재하는지 아닌지는 잘 모르겠지만, 아직 답을 찾지 못해서 사과를하지 못했습니다. 여기

지금 여기에 내가 UDF는 회원의 정보를 필요

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

테이블 생성 과정에 대해 가지고 무엇

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

먼저 생성 된 UDF이며 다음 계산 자신의 '큐피드 '그 점수는 다른 모든 것과 함께 테이블에 삽입됩니다.

+1

을 작동하려면 선언하고 당신이 필요하다고 생각 어떤 변수를 설정해야합니다, 당신의 UDF 나 빨리 당신이 충분히에 시작하는 주어 호출하는 트리거를 사용하여 테이블에 데이터 삽입 ... – Jesuraja

+0

UDF는 7 개의 매개 변수를 예상합니다. 나는 그것을 전달할 필요가있다 – cha

답변

1

시도는 큐피드 열이를 사용하기 좋을 것 UDF를 사용하여 어떤 도움 : 나는 매우 심각하게 삽입에 UDF의 호출을 이동 제안

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

/을 대신 업데이트 테이블 정의. 이것을 쉽게하기 위해 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 
0

나는 당신이 VUW에 있다고 가정하고, 당신 자신이 341을하고 있다고 가정하고 있습니다. 당신이 당신은`시도 TRIGGER`있는 동안 수는

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




UPDATE DBO.Member_Profile 
SET Cupid_Score = @Cupid_Score WHERE [email protected] 

END 
+0

위에서 보았 듯이 UDF를 테이블 열로 사용하고 삽입과 동시에 점수를 계산하는 방식이 조금 다르게 수행되었다. – James

+0

@James : 당신은 사빈 바이오의 대답에서했던 것처럼 말한 겁니까? –

+0

@AndriyM 예 했어 – James