当前位置:网站首页>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
边栏推荐
- 2022-2028 global ICT test probe industry research and trend analysis report
- 2022-2028 global elevator emergency communication system industry research and trend analysis report
- Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
- CentOS installation starts redis
- CTF tool (1) -- archpr -- including installation / use process
- C#生成putty格式的ppk文件(支持passphrase)
- Line number of Jenkins pipeline script execution exception
- Tibetan poem PTA
- Tide - rust web framework based on async STD
- The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?
猜你喜欢

2022-2028 global 3D printing ASA consumables industry research and trend analysis report

P4学习——Basic Tunneling

Implementation of OSD on Hisilicon platform (1)

Authentication principle of Ranger plug-in
![[UML] UML class diagram](/img/6f/30bd15967103969e600d69e618d8bf.png)
[UML] UML class diagram

Wordpress blog uses volcano engine veimagex for static resource CDN acceleration (free)

C#生成putty格式的ppk文件(支持passphrase)

2022-2028 global single travel industry research and trend analysis report

2022-2028 global retro glass industry research and trend analysis report

Why should VR panoramic shooting join us? Leverage resources to achieve win-win results
随机推荐
PyTorch安装并使用gpu加速
The principle and related problems of acid in MySQL
C language file operation for conquering C language
What SQL statements are supported for data filtering
C#生成putty格式的ppk文件(支持passphrase)
Design e-commerce seckill system
Redis - understand the master-slave replication mechanism
Tide - rust web framework based on async STD
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
NE555波形发生器手把手教程之NE555内部结构(一)
Cloud security daily 220630: the IBM data protection platform has found an arbitrary code execution vulnerability, which needs to be upgraded as soon as possible
Bugku CTF daily one question dark cloud invitation code
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
HDU 2488 A Knight's Journey(DFS)
2022-2028 global herbal diet tea industry research and trend analysis report
CSDN常用复杂公式模板记录
解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
合适的工作就是好工作
2022-2028 global encrypted external hard disk industry research and trend analysis report
New trend of embedded software development: Devops