我有两张桌子:students
,ece200_atten
。
students
有两列:id, ece200
,但ece200
是bit
=>真或假ece200_attend
有一列id
当我插入id
时,我想检查(如果此id = 1的ece200)在表ece200_attend中插入该id。
我的代码:
alter proc test
@myid int
as
begin
declare @mycourse bit = 1
select @mycourse = ece200 from students
if @mycourse = 1
insert into ece200_attend (id) values (@myid)
end
go
exec test 34003
答案 0 :(得分:2)
if exists (select * from students where id = @myid and ece2000 = 1)
begin
insert into ece200_attend (id) values (@myid)
end
答案 1 :(得分:1)
试试这个,
ALTER PROC test(@myid int)
AS BEGIN
IF EXISTS(SELECT 1 FROM STUDENTS WHERE ece200=1)
INSERT INTO ece200_attend (id) values (@myid)
GO
EXEC TEST 34003