sql高级2

 1 create proc proc_getClass   @SchoolCode int
 2 as
 3 select * from dbo.TClass where SchoolCode=@SchoolCode
 4 exec proc_getClass @SchoolCode
 5 go
 6 create proc proc_getSchool 
 7 as
 8 select * from dbo.TSchool
 9 exec dbo.proc_getSchool
10 go
11 create proc proc_AddStudent  @StudentName nvarchar(max), @ClassCode int, @Gender int
12 as
13 begin
14 begin tran
15 begin try
16 insert into dbo.TStudent(StudentName, ClassCode, Gender)values(@StudentName, @ClassCode, @Gender)
17 commit tran
18 end try
19 begin catch
20 rollback tran
21 end catch
22 end
23 exec proc_AddStudent @StudentName, @ClassCode, @Gender
24 go
25 alter proc proc_getStudent @StudentName nvarchar(max),@ClassCode int,@SchoolCode int,@Dpage int,@Count float output
26 as
27 declare @c float,@sql nvarchar(max),@sql1 nvarchar(max)
28 set @sql1='select ROW_NUMBER() over(order by(StudentId))lid,s.Gender,s.StudentName,s.StudentId,c.CLassName,sc.SchoolName,c.ClassCode,sc.SchoolCode from dbo.TStudent s join dbo.TClass c on s.ClassCode=c.ClassCode join dbo.TSchool sc on sc.SchoolCode=c.SchoolCode where 1=1'
29 if(@StudentName!='')
30 set @sql1+=' and s.StudentName like ''%'+@StudentName+'%'''
31 if(@SchoolCode!=0)
32 set @sql1+=' and sc.SchoolCode ='+CONVERT(nvarchar(50),@SchoolCode)
33 if(@ClassCode!=0)
34 set @sql1+=' and c.ClassCode ='+ CONVERT(nvarchar(50),@ClassCode)
35 set @sql='select * from ('+@sql1+') tt where lid between ' +CONVERT(nvarchar(50),(@Dpage-1)*3+1)+' and '+CONVERT(nvarchar(50),@Dpage*3)
36 begin tran
37 begin try
38 exec (@sql)
39 exec(@sql1)
40 set @c=@@ROWCOUNT
41 set @Count=ceiling(@c/3.0)
42 commit tran
43 end try
44 begin catch
45 rollback tran
46 end catch
47 exec dbo.proc_getStudent '',1,1,1,1 
48 go
49 create proc proc_Delete @StudentId int
50 as
51 begin tran
52 begin try
53 delete from dbo.TStudent where StudentId=@StudentId
54 commit tran
55 end try
56 begin catch
57 rollback tran
58 end catch
59 exec dbo.proc_Delete @StduentId
60 go
61 create proc proc_update @StudentId int, @ClassCode int, @StudentName nvarchar(max), @Gender int
62 as
63 begin tran
64 begin try
65 update  dbo.TStudent set ClassCode=@ClassCode,StudentName=@StudentName,Gender=@Gender where StudentId=@StudentId
66 commit tran
67 end try
68 begin catch
69 rollback tran
70 end catch
71 exec dbo.proc_update @StudentId, @ClassCode , @StudentName, @Gender
72 go
73 create proc get_fan @StudentId int
74 as
75 begin
76 begin tran
77 begin try
78 select s.StudentName,s.Gender,s.StudentId,s.Gender,c.SchoolCode from dbo.TStudent s join dbo.TClass c on s.ClassCode=c.ClassCode where StudentId=@StudentId
79 
80 commit tran
81 end try
82 begin catch
83 rollback tran
84 end catch
85 end
86 exec get_fan @StudentId 

猜你喜欢

转载自www.cnblogs.com/mamingyuan/p/9387901.html