Stored procedures and triggers

1, create a trigger named score_sc, requiring score entered must be between 0-100

Flag create trigger score_sc --trigger flip-flop, flip-flop name for score_sc

on sc - sc role in the above table

for insert, update - is inserted into the operating range and update

as declare @score int --declare used to declare variables 

@Score variable declaration, int type is declared begin - begin each correspond to a end otherwise it will error, the trigger three begin and end three

select @score = grade from inserted - declare variables equal grade, inserted to check how to update identifier

if @score not between 0 and 100 - the trigger condition

begin rollback - rollback

print 'Enter the result is invalid, please try again'

end

else

begin print 'operation is successful!'

end

end   

2, create a stored procedure based on the number of elective courses name queries in this course                            

 create proc ssc_3 --procedure marked the stored procedure, ssc_3 is the name of the stored procedure

@cnumber char (20), @ ccount int output - to declare a variable @cname type char, variable @ccount, of type int, output is output

Length as --char If not, then check out the result will be 0

select @ccount=count(*) from sc,course

sc.cno = course.cno and the WHERE course.cname=@cnumber   - go for the variable declaration of assignment                                 

The following statements create a stored procedure execution, the following three statements to be executed together, or can not get the results

declare @ccount int            

exec ssc_3 'database', @ ccount output --exec identifier is executed, ssc_6 to the name of the stored procedure, the value of a database variable @ccount

select 'number of students is:', @ ccount

Reproduced in: https: //www.cnblogs.com/goforitjava/p/3478505.html

Guess you like

Origin blog.csdn.net/weixin_33768481/article/details/94290981