当前位置:网站首页>SQLSERVER database application and development: Chapter 9 computer operation

SQLSERVER database application and development: Chapter 9 computer operation

2022-06-10 22:36:00 White in white

Code and explanation

9.1:

create procedure StuInfo3
as
select studentno,sname,birthdate,phone
from student
where studentno like '18%'
go
exec StuInfo3

9.2

create procedure ScoreInfo
as
select student.studentno,sname,sex,cname,final
from student,course,score
where student.studentno=score.studentno and score.courseno=course.courseno
go
exec ScoreInfo

9.3

if exists(
select name from sysobjects
where name= 'stu_age' and type= 'p'
)
drop procedure stu_Age
go

create procedure stu_Age 
@studentno nvarchar(10),@age int output
as
declare @errorvalue int
set @errorvalue=0
select @age=year(GETDATE())-year(birthdate)
from student
where studentno=@studentno
if(@@ERROR<>0)
	set @errorvalue=@@ERROR
return @errorvalue
go

declare @average int
exec stu_age '1',@age=@average output
select @average
  1. procedure It's equivalent to a function , There are two parameters , You can set parameters in the function body , Set in the code @age= Age , Easy to return ;@studentno Used for matching
  2. @@error It's not equal to 0 There was an error , Error number returned
  3. @average Used to receive the returned @ave
  4. type='p’ Show as prodecure
    9.4
create trigger stu_insert
on student
after insert
as
print ' You inserted a new record '
go

9.5

CREATE TRIGGER TR_ScoreCheck
ON score
FOR INSERT, UPDATE
AS
IF UPDATE(final )
PRINT 'AFTER The trigger starts executing ……'
BEGIN
DECLARE @ScoreValue real
SELECT @ScoreValue=(SELECT final FROM inserted)
IF @ScoreValue>100 OR @ScoreValue<0
PRINT ' Incorrect score entered , Please confirm the entered test score !'
END
GO
  1. insert and update Statement after the corresponding trigger is activated , All records added or updated are saved to inserted In the table
  2. delete and update Statement after the corresponding trigger is activated , All deleted records are saved to deleted In the table
  3. for amount to after

9.6

if exists(select name from sysobjects where name='dela' and type='tr')
drop trigger dela
go
create trigger dela
on course
after delete
as
print' Can't delete '
  1. type='tr’ Expressed as trigger
原网站

版权声明
本文为[White in white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102122167577.html