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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Sqlserver 2005 데이터베이스 추가 시 오류 알림 운영 체제 오류 5(접근 거부)오류 5120 해결 방법최근 며칠 간 인터넷 에서 asp.net 로그 인 사례 를 찾 아 연구 코드 를 연구 하려 고 했 더 니 Sql Server 2005 로 데이터베이스 파일 을 추가 할 때 오류 메시지 가 나 왔 다.네티즌 들 은 A...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.