当前位置:网站首页>Experiment 8 T-SQL, stored procedure
Experiment 8 T-SQL, stored procedure
2022-07-01 00:39:00 【Askar code】
Experiment 8 T-sql, stored procedure
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’)
Print ‘03 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 = ‘2005150026’
If (@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=’200515002’
If (@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 = ‘200515023’
If (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)print ‘0’
Else print ‘1’
End
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
边栏推荐
- P4学习——p4runtime
- 优质的水泵 SolidWorks模型素材推荐,不容错过
- Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
- Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
- MySQL variables, stored procedures and functions
- leetcode 474. Ones and Zeroes 一和零(中等)
- Confirm() method of window
- CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
- To tell you the truth, ThreadLocal is really not an advanced thing
- 深度学习的历史
猜你喜欢
Two-stage RO: part 1
Which is better, server rental or hosting services in the United States?
2022-2028 global electric yacht industry research and trend analysis report
The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
2022-2028 global PTFE lined valve industry research and trend analysis report
优质的水泵 SolidWorks模型素材推荐,不容错过
Deployment of mini version message queue based on redis6.0
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
随机推荐
Oracle-数据完整性
Wechat official account development (1) introduction to wechat official account
Authentication principle of Ranger plug-in
Plot size and resolution with R markdown, knitr, pandoc, beamer
ArrayList分析1-循环、扩容、版本
How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
问题解决:如何管理线程私有(thread_local)的指针变量
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
2022-2028 global encrypted external hard disk industry research and trend analysis report
2022-2028 global electric yacht industry research and trend analysis report
MySQL index test
[untitled]
[designmode] singleton pattern
C#生成putty格式的ppk文件(支持passphrase)
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
[UML] UML class diagram
New trend of embedded software development: Devops
The programmer's girlfriend gave me a fatigue driving test
[PHP] self developed framework qphp, used by qphp framework
Redis - sentinel mode