求一个存储过程(a,b,c三个表;b和a有主外键,c和b、a有主外键)
表名和表结构如下: 
 a   (id,name) 
 b(id,name,aid《a表id》) 
 c(id,name,aid《a表id》,bid《b表id》)   
 存储过程要求: 
 如果传入一个aid的话,如果相对应的b表没有任何相关记录则返回false,然后判断c表中如果aid下关联的任意一个b表的记录在c表找不到关联的记录那么返回false。   
 举个例子   我现在有3张表分别是公司表,部门表,人员表 
 公司表(id,name) 
 部门表(id,name,公司id) 
 职员表(id,name,公司id,部门id) 
 那么现在如果通过传入的一个公司id, 
 1   首先判断该公司id是否能找到记录,如果找不到返回none   enterprise    
 2   然后来判断该公司下是否有任何部门,没有一个部门则返一个值(none   depart) 
 3   最后如果对应公司id下的部门中有个部门下没有一个职员,则也返回一个值(none   employee) 
 4   如果上述情况都没出现返回成功(success)
------解决方案--------------------create proc proc_test(@id int,@retValue varchar(20) output) 
 as 
 if not exists(select 1 from a where id=@id) 
 begin 
 set @retValue= 'none enterprise ' 
 return 
 end 
 if not exists(select 1 from b where aid=@id) 
 begin 
 set @retValue= 'none depart ' 
 return 
 end 
 if exists(select 1 from b Left join c on b.id=c.bid where b.aid=@id and c.id is null) 
 begin 
 set @retValue= 'none employee ' 
 return 
 end 
 set @retValue= 'success ' 
 return
------解决方案----------------------創建測試環境 
 Create Table 公司表(id Int, name Varchar(10)) 
 Create Table 部门表(id Int, name Varchar(10), 公司id Int) 
 Create Table 职员表(id Int, name Varchar(10),公司id Int, 部门id Int) 
 Insert 公司表 Select 1,  'G1 ' 
 Union All Select 2,  'G2 ' 
 Union All Select 3,  'G2 '   
 Insert 部门表 Select 1,  'B1 ', 1 
 Union All Select 2,  'B2 ', 1 
 Union All Select 3,  'B3 ', 2   
 Insert 职员表 Select 1,  'Z1 ', 1, 1 
 Union All Select 2,  'Z2 ', 1, 2 
 GO 
 --創建存儲過程 
 Create ProceDure SP_TEST(@id Int, @Result Varchar(100) Output) 
 As 
 	If Not Exists(Select id From 公司表 Where id = @id) 
 		Select @Result =  'none enterprise '		 
 	Else 
 	Begin 
 		If Exists(Select A.id From 公司表 A Left Join 部门表 B On A.id = B.公司id Where B.公司id Is Null And A.id = @id) 
 			Select @Result =  'none depart ' 
 		Else 
 		Begin 
 			If Exists(Select A.id From 公司表 A Inner Join 部门表 B On A.id = B.公司id Where Not Exists(Select id From 职员表 Where 公司id = A.id And 部门id = B.id) And A.id = @id) 
 				Select @Result =  'none employee ' 
 			Else 
 				Select @Result =  'success ' 
 		End 
 	End 
 GO 
 --測試 
 Declare @Result Varchar(100) 
 EXEC SP_TEST 5, @Result Output 
 Select @Result 
 EXEC SP_TEST 3, @Result Output 
 Select @Result 
 EXEC SP_TEST 2, @Result Output 
 Select @Result 
 EXEC SP_TEST 1, @Result Output 
 Select @Result 
 GO 
 --刪除測試環境 
 Drop Table 公司表, 部门表, 职员表 
 Drop ProceDure SP_TEST 
 --結果 
 /* 
 none enterprise 
 none depart 
 none employee 
 success 
 */