当前位置:网站首页>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
边栏推荐
- Gateway service gateway
- 问题解决:如何管理线程私有(thread_local)的指针变量
- When is it appropriate to replace a virtual machine with a virtual machine?
- SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
- P4学习——Basic Tunneling
- Date类的实现
- 2022-2028 global retro glass industry research and trend analysis report
- Yboj mesh sequence [Lagrange interpolation]
- Multi graph explanation of resource preemption in yarn capacity scheduling
- File reading and writing for rust file system processing - rust Practice Guide
猜你喜欢

魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计

Ranger plug-in development (Part 2)

Deployment of mini version message queue based on redis6.0

Teach you how to use Hal library to get started -- become a lighting master

What should I do without 50W bride price

New trend of embedded software development: Devops

Why should VR panoramic shooting join us? Leverage resources to achieve win-win results

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

2022-2028 global capsule shell industry research and trend analysis report

Confirm() method of window
随机推荐
PyTorch安装并使用gpu加速
20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
ArrayList分析1-循环、扩容、版本
MySQL variables, stored procedures and functions
Luogu p1168 median
Oracle临时表详解
[DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
JS bubble sort and select sort
Can SQL execution be written in tidb dashboard
How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?
Date类的实现
2022-2028 global encrypted external hard disk industry research and trend analysis report
New trend of embedded software development: Devops
BeanUtils. Copyproperties() vs. mapstruct
20220216 misc buuctf backdoor killing (d shield scanning) - clues in the packet (Base64 to image)
20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
leetcode 474. Ones and Zeroes 一和零(中等)
Confirm() method of window
2022-2028 global ethylene oxide scrubber industry research and trend analysis report
Luogu p1144 shortest circuit count