当前位置:网站首页>Experiment 8 T-SQL, stored procedure

Experiment 8 T-SQL, stored procedure

2022-07-01 00:39:00 Askar code

1. Use of variables

Experimental code 、 Experimental process ( Screenshot in interface mode )、 experimental result ( Screenshots ) (1) Create local variables @xh(char(9)) And use set take student In the table “ Li Yong ” The student number field of is assigned to the variable , And then use select Display the value of the variable ;

Decalre @xh char(9)
Set @xh = (select sno from student where sname=’ Li Yong ’)
Select @xh as  Student number 

(2) Calculate student information sheet student The difference between the highest and lowest ages of middle school students , And use select Assign the result to @cz, And use print Display the value of the variable .

Declare @cz int
Select @cz=max(sage)-min(sage) from  student
Print @cz

2. Apply built-in functions

Experimental code 、 Experimental process ( Screenshot in interface mode )、 experimental result ( Screenshots ) (1) seek course The course number in the table is “03” The length of the course name , And output the results in the message prompt box . The result format is :“03 The length of the course name is :**”.
Tips : Use CONVERT function .

Declare @length int
Set @length = len(select cname from course where cno=3)
Print03 The length of the course name is :’+str(@length,1)

(2) Use the string concatenation operator “+”, take student All fields in the table are concatenated with a “ Student information ” Fields are displayed .

Select sname+’ ’+convert(varchar)+’ ’+sdept  as  ‘ Student information ’
From student

3. Flow control statement

Experimental code 、 Experimental process ( Screenshot in interface mode )、 experimental result ( Screenshots ) (1) Judge student Does the table have a number 2005150026 Of the students , If there is , Then the basic information of the student is displayed ; If it does not exist , Is displayed “ Check no one ”.

Declare @str 
Set @str =2005150026If (@str in (select sno from student))
Begin
Select * from student where sno = @str 
End
Else
Print ‘ Check no one ’

(2) The student ID is “200515002” Whether the average score of students exceeds 85 branch , If it exceeds, output “XX( The student's name ) Got a high score in the exam ”, Otherwise output “XX( The student's name ) Average test ”.

Declare @name varchar(10),@pjf int
Select @name = (select sname from student where sno =200515002)
Set @pjf =  avg(grade) from sc where sno=200515002If (@pjf>85) print @name+‘ Got a high score in the exam ’
Else print @name+‘ Average test ’

4. Defined function

Experimental code 、 Experimental process ( Screenshot in interface mode )、 experimental result ( Screenshots ) (1) Define a function CHECK_SNO Achieve the following functions : For a given sno value , Query the value in student Is there... In the table , If it exists, it returns 0, Otherwise return to -1.

Create function CHECK_SNO(@sno varchar(10))
Return int
As
Begin
If (@sno in (select sno from student))
Return 0
Else
Return -1
End

(2) Write a paragraph T-sql The program calls the above functions . Direction sc When the table inserts a row of records , Call the function first CHECK_SNO Retrieve the of this record sno The values are in the table student Whether there is a corresponding value in , If exist , Then insert the record into the record (‘200515023’,’02’,75) To sc In the table .

Declare @sno
Set @sno =200515023If (CHECK_SNO(@sno)==0)
Begin
Insert into sc values(200515023,02,75)
End

(3) Define a function , Calculate the average age of all current students by Department , And call this function to find “CS” The average age of students in the Department .

Create fnuction f(@sdept varchar(10))
Return int
As
Begin
Declare @pjnl int
Select @pjnl = avg(sage) from student where sdept=@sdept
Return @pjnl
End
Declare @sdept varchar(10),@cnt int
Set @sdept =’CS’
Set @cnt = f(@sdept)
Select @cnt

5. stored procedure

Experimental code 、 Experimental process ( Screenshot in interface mode )、 experimental result ( Screenshots ) (1) Create a parameterless stored procedure pr_StuScore, Query the following information : class 、 Student number 、 full name 、 Gender 、 Course name 、 Test results .

Create proc pr_StuScore
As
Select  a.sdept,a.sno,a.sname,a.sex,b.cname,c.grade
From student a,course b,sc c
Where c.sno=a.sno and c.cno=b.cno

(2) Creates a stored procedure with parameters pr_StuScoreInfo2, The stored procedure queries the following information according to the incoming student number and course name : class 、 Student number 、 full name 、 Gender 、 Course name 、 Test results .

Create proc pr_StuScoreInfo2(@sno varchar(10),@cname varchar(10))
As
Select a.sdept,a.sno,a.sname,a.sex,b.cname,c.grade
From student a,course b,sc c
Where a.sno = c.sno and b.cno = c.cno and a.sno = @sno and b.cname = @cname

(3) Creates a stored procedure with parameters pr_xxscore, The stored procedure is based on the incoming student number and course number , And output the student's course score through variables .

Create proc pr_xxscore(@sno varchar(10),@cno varchar(10),@res int output)
As
Select @res = grade from sc where sno=@sno and cno=@cno

(4) Create stored procedure , By giving the student numbers of two students , Compare the ages of the two students , If the former is higher than the latter, output 0, Otherwise output 1.( Compare when calling 200515001 Number and 200515002 Age of No ).

Create proc pr_compare(@sno1 varchar(10),@sno2 varchar(10))
As
Begin
Declare @age1,@age2
Select @age1 = age from student where sno=@sno1
Select @age2 = age from student where sno=@sno2
If(@age>@age2)print0Else print1End
Exec pr_compare ‘200515001,200515002

(5) Write a stored procedure with parameters pr_cavg, Count the average score of the course according to the incoming course name .

Create proc pr_cavg(@cname varchar(10),@res int output)
As
Begin
Select @res = avg(grade) 
From coure a,sc c 
Where a.cno = c.cno and a.cname = @cname  
End

(6) Create a stored procedure pr_age, It is used to count the student numbers of all students of a certain age 、 Name and selected course information .

Create proc pr_age(@age1 int ,@age2 int)
As
Begin
Select a.sno,a.sname,b.cno,b.cname,b.ccredit
From student a,course b,sc c
Where (a.sage between @age1 and @age2 ) and a.sno=c.sno and b.cno = c.cno
End

(7) Create a stored procedure to add student records stduentadd, To give student Table add a record .(‘200515028’,‘ Xu Xiaoming ’,‘ male ’,24,‘CS’)

Create proc studentadd(@sno varchar(10),@sname varchar(10),@sex varchar(2),@age int,@sdept varchar(10))
As
Begin
Insert into student values(@sno,@sname,@sex,@age,@sdept)
End
原网站

版权声明
本文为[Askar code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202206302351509886.html