1 declare @studentid int //聲明一個int型變量 2 set @studentid=(select id from tclass where classid= 1);//設置變量值,tclass 為任意表,classid 為表中任一字段 3 if(@studentid is not null)//如果值不為空,則執行插入語句,向表tstudent插入數據 4 INSERT INTO tstudent (studentid,phoneNo)VALUES (@studentid,@phoneNo);