当前位置:网站首页>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
边栏推荐
- Ybtoj exchange game [tree chain splitting, line segment tree merging]
- 2022-2028 global encrypted external hard disk industry research and trend analysis report
- Vmware16 installing win11 virtual machine (the most complete step + stepping on the pit)
- Don't worry about whether you can be a coder if you don't learn English well. Learn it first
- Unit test concept and purpose
- How to specify the number of cycles in JSTL- How to loop over something a specified number of times in JSTL?
- Yboj mesh sequence [Lagrange interpolation]
- IFLYTEK active competition summary! (12)
- A detailed explanation of the implementation principle of go Distributed Link Tracking
- Luogu p1168 median
猜你喜欢

C language file operation for conquering C language

2022-2028 global ICT test probe industry research and trend analysis report

Implementation of OSD on Hisilicon platform (1)

Bugku CTF daily one question dark cloud invitation code

How to edit special effects in VR panorama? How to display detailed functions?

ABAQUS 2022 software installation package and installation tutorial

2022-2028 global encrypted external hard disk industry research and trend analysis report

20220215 misc buctf easycap Wireshark tracks TCP flow hidden key (use of WinHex tool)

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

【日常记录】——对BigDecimal除法运算时遇到的Bug
随机推荐
Interface documentation system - Yapi
Redis based distributed lock
2022-2028 global electric yacht industry research and trend analysis report
【日常记录】——对BigDecimal除法运算时遇到的Bug
剑指 Offer 18. 删除链表的节点
MySQL variables, stored procedures and functions
SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
2022-2028 global rotary transmission system industry research and trend analysis report
C#生成putty格式的ppk文件(支持passphrase)
Manage edge browser settings (ie mode, homepage binding, etc.) through group policy in the enterprise
Operation record of reinitialization instance of Dameng database
Multi graph explanation of resource preemption in yarn capacity scheduling
Thoughts on the future of data analysis in "miscellaneous talk"
Teach you how to use Hal library to get started -- become a lighting master
Explain kubernetes backup and recovery tools velero | learn more about carina series phase III
File reading and writing for rust file system processing - rust Practice Guide
实验八 T-sql,存储过程
Redis - sentinel mode
2022-2028 global herbal diet tea industry research and trend analysis report
"Experience" my understanding of user growth "new users"