2014-02-22 72 views
1

我有2個表格:students,ece200_atten存儲過程中的IF條件

  • 第一臺students有2列:id, ece200,但ece200bit => true或false
  • 二表ece200_attend有一欄id

當我插入id我要檢查(如果這個ID的ece200 = 1)在表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

所以什麼問題你的代碼? – KumarHarsh

回答

2
if exists (select * from students where id = @myid and ece2000 = 1) 
    begin 
    insert into ece200_attend (id) values (@myid) 
    end 
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