如何检查另一个表中的值

时间:2014-07-30 18:54:55

标签: sql-server

我有桌子:

create table Authorized(
diver_number int not null,
level_name char(30) not null,
constraint PK_Authorized primary key (diver_number, level_name),
Constraint FK_diver_number1 foreign key (diver_number) references Diver(diver_number)
on update cascade on delete cascade,
Constraint FK_level_name foreign key (level_name) references Level(name)on update
cascade on delete cascade,
club_number int Constraint FK_club_number foreign key (club_number) references DivingClub(number)
on update cascade on delete cascade not null,
authorization_date date not null,
picture image)

并且可以:

create table Works_for(
diver_number int not null,
club_number int not null,
constraint PK_Works_for primary key (diver_number, club_number),
Constraint FK_diver_number2 foreign key (diver_number) references Diver(diver_number)
on update cascade on delete cascade ,
Constraint FK_club_number2 foreign key (club_number) references DivingClub (number)on     update
cascade on delete cascade,
start_working_date date not null,
end_working_date date)

当我将diver_number添加到表Works_for时,我想检查这个潜水员是否是"指南" (授权表中的level_name)。我怎么检查呢?

1 个答案:

答案 0 :(得分:0)

我认为你的标量函数更像是这样。

CREATE FUNCTION dbo.checkADate(@diver_number int) RETURNS BIT AS 
BEGIN
    declare @Found bit 

    select @Found = count(*)
    from Authorized 
    WHERE diver_number = @diver_number 
        and level_name = 'Guide'

    return @Found 

END