sqlserver 저장 프로 세 스 학습 (알 기 쉬 움)

--------------------------------------------------------------------------
-------------------------------    Study------------------------------
--------------------------------------------------------------------------
--   
drop table student
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--         
create table student
(
	id int identity(1,1),--  1,
	stuNo varchar(50) primary key, --   
	stuName varchar(50),
	stuAge datetime,
	stuSex varchar(5)		
)

--         
create table course
(
	id int identity(1,1),--  1,
	stuNo varchar(50),
	courseName varchar(50),
	courseScore decimal	
)

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--    
--      
insert into student(stuNo,stuName,stuAge,stuSex) values('01','  ','1996-08-27 09:00:00.000',' ')

--       
--SQL Server2008     
insert into student values('02','  ','1995-4-20 6:0:0',' '),
						  ('03','  ','1988-9-17 15:30:0',' ')
						  
insert into course values('02','    ','85.5'),
						 ('02','  ','70'),
						 ('02','  ','80'),
						 ('02','  ','90'),
						 ('02','  ','65'),
						 ('02','  ','96')
						 
insert into course values('03','    ','60'),
						 ('03','  ','65'),
						 ('03','  ','84'),
						 ('03','  ','70'),
						 ('03','  ','76'),
						 ('03','  ','54')						 
						  

--  UNION ALL     
insert into student 
select '04','wangan','1895-5-27 14:30:28',' '
union all 	
select '05','zhangnan','1990-1-20 19:0:0',' '
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--    
update student set stuSex=' ',stuAge='2016-5-9 8:0:0' where stuName='  ' 
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--    
delete from student where stuNo=01
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--          
--begin...end         {}
create proc stu1
as
begin
	select * from student;
end
go

exec stu1
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                 
create proc stu2
@sname varchar(50)   --      
as
begin
	select * from student s where s.stuName=@sname;
end
go

exec stu2 '  '
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                 
create proc stu3
@sname varchar(50)='  '
as 
begin
	select * from student s where s.stuName=@sname; 
end
go

exec stu3 
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                     
create proc stu4
@sname varchar(50),
@result varchar(8) output --    
as
begin
	 if (select COUNT(1) from student s where s.stuName=@sname)>0
	 --if exists (select COUNT(1) from student s where s.stuName=@sname)
	 set 
		@result='  !'
	 else
	 set 
		@result='   !'
end
go


declare @result varchar(8)
exec stu4 '  1',@result output
print @result
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                      
create proc stu5
as
declare @sname varchar(50)  --      
set @sname='  '
begin
	select * from student s where s.stuName=@sname
end
go

exec stu5
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                 
create proc stu6
@stuNo varchar(50)
as
declare @sname varchar(50)
set @sname=(select s.stuName from student s where s.stuNo=@stuNo)
select @sname
go

exec stu6 '01'
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--          
create proc stu7
@stuNo varchar(50),
@stuName varchar(50),
@stuAge datetime,
@stuSex varchar(5)
as 
begin
	insert into student 
		(stuNo,stuName,stuAge,stuSex)
	values 
		(@stuNo,@stuName,@stuAge,@stuSex) 
end
go

exec stu7 '07','  ','2000-9-9 9:9:9',' '
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                   
--@@rowcount      
--return    ,       
create proc stu8
@stuName varchar(50)
as
begin
	delete from student where stuName=@stuName
	return @@rowcount
end
go

declare @result varchar(50)
exec @result=stu8 '  '
select @result as '    '
--print @result
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                  
create proc stu9
@stuNo varchar(50),
@avg int output
as 
begin
	set @avg=(select AVG(courseScore) from course where stuNo=@stuNo)
	--  
	--select @avg=AVG(courseScore) from course where stuNo=@stuNo
end
go

declare @avg int
exec stu9 '02',@avg output
print @avg
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                        
create proc stu10
@stuNo varchar(50)
as
	select c.stuNo,s.stuName,s.stuAge,s.stuSex,c.courseName,c.courseScore from student s join course c on s.stuNo=c.stuNo where s.stuNo=@stuNo
go

exec stu10 '02'
go

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--            ,   ,      ,    ;  ,    
create proc stu11
@stuNo varchar(50),
@stuName varchar(50),
@stuAge datetime,
@stuSex varchar(5),
@result varchar(50) output
as
	if exists (select * from student where stuNo=@stuNo)
		begin
			set @result='   ,     !'
		end		
	else
		begin
			insert into student 
				(stuNo,stuName,stuAge,stuSex)
			values 
				(@stuNo,@stuName,@stuAge,@stuSex)
			set @result='   ,        !'	 
		end
go

declare @result varchar(50)
exec stu11 '06','   ','1980-8-8 8:9:0',' ',@result output
print @result

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--                           
create proc stu12
@stuNo varchar(50)
as
declare @curAvg decimal(18,2)
declare @totalAvg decimal(18,2)
if exists(select * from course where stuNo=@stuNo)
	begin
		set @totalAvg=(select AVG(courseScore) from course)
		select @curAvg=AVG(courseScore) from course where stuNo=@stuNo
		print ('     :'+convert(varchar(18),@totalAvg))
		print ('      :'+convert(varchar(18),@curAvg))
		if @curAvg>@totalAvg
			print '      !'
		else 
			print '      !'	
	end
else
	print '            ,     !'
go

exec stu12 '03'
go

좋은 웹페이지 즐겨찾기