SQL server 變數if,while,預存程序

來源:互聯網
上載者:User

標籤:

一.變數

1.if迴圈

 

 

 

 

 

 

 

 

2.

3.while迴圈

  declare @ss int
  set @ss =2
while @ss<10
begin
  print ‘Hello‘
  set @[email protected]+1
end
  --break 跳出迴圈

  declare @sss int
  set @sss=2
  while @sss<10
begin
  print‘Hello‘
  set @[email protected]+1
if @sss=6
  break
end
  --continue 跳出本次迴圈,繼續下次迴圈

  declare @ssss int
  set @ssss=2
while @ssss<10
begin
  set @[email protected]+1
if @ssss>4and @ssss<7
  continue
  print‘Hello‘
end

4.練習,查看迴圈的過程

5.case when --相當於if 但是有局限性,需要具體到值
--查詢老師表的年齡,如果>=35,【老頭子】
--<=30【小青年】
--其他,【正當年】
select age,
case age
when 35 then ‘老頭子‘
when 36 then ‘老頭子‘
when 37 then ‘老頭子‘
when 38 then ‘老頭子‘
when 30 then ‘小青年‘
when 29 then ‘小青年‘
when 28 then ‘小青年‘
when 27 then ‘小青年‘
else ‘正當年‘
end
from teacher

6.查詢英語分數最高的學生的性別,若男,【這是一個男生】
--若女,【這是一個小姑娘】
--若英語分數超過90,【成績非常好,繼續保持】
  declare @xing char(10)
  select @xing =sex from students where code =(select top 1 code from score order by yingfen desc)
  declare @yingfen int
if @xing =‘男‘
  print‘這是一個男生‘
else if @xing =‘女‘
  print‘這是一個小姑娘‘
else if @yingfen>90
  print‘成績非常好,繼續把持‘

7.查詢數學分數最高的學生的任課教師的姓名和科目,
--【**老師教課品質高,是個**教師!】
declare @jname varchar(50)
declare @lesson char(10)
select @jname=name from teacher where code=
(select shujiao from Student where code=
(select top 1 code from score order by shufen desc) )

select @lesson=lesson from teacher where name [email protected]
print @jname +‘老師教課品質高‘+‘是個‘+rtrim(@lesson)+‘教師‘

7.查詢三班語文教師的工號,姓名,以及所教科目
select code,lesson,name from teacher where code=(select top 1 yujiao from student where banji=‘三班‘ order by yujiao)

8.查詢總分最高的學生的語文教師的所有資訊
select * from teacher where code =
(select yujiao from student where code =
(select top 1 code from score group by code order by SUM(shufen+yufen+yingfen)desc))

 二.儲存

1.create proc firstproc--建立一個預存程序
as --預存程序關鍵字
select * from student--預存程序的語句
go
--執行預存程序的語句(兩個都可以)
exec firstproc
execute firstproc

--預存程序可以有傳回值
--定義一個變數去接收
declare @fanhui int
exec @fanhui = firstproc--需要執行之後才會有傳回值,用變數接收
select @fanhui as 傳回值--查看傳回值

--修改預存程序的語句
alter proc firstproc
as
select * from score
go

練習: 利用預存程序尋找語文教師張曉華所教課程的學生的分數
        過80的算優秀,優秀人數超過3個人即為【教師評測達標】
        若不到3個人,【不達標】
  create proc thirdproc
as
  declare @code int
  select @code=code from teacher where name =‘張曉華‘
  declare @count int
  select @count =COUNT(*)from score where code in (select code from students where yujiao [email protected])and yufen>80
if @count>3
  print‘教師評測達標‘
else
  print‘教師評測不達標‘
go
  exec thirdproc

2.

 

 

 

 

 

3.

 

練習:預存程序
查看所輸入編號的學生是否能夠結業,兩門以上及格即可結業
三門都及格,【優秀】
兩門及格,【結業】
一門及格,【不結業】
三門都不及格,【請重修】
  create proc sevenproc
  @code int
as
  declare @yufen decimal(18,2),@shufen decimal(18,2),@yingfen decimal(18,2)
  select @yufen =yufen from score where code [email protected]分別查詢語數英的分數
  select @shufen =shufen from score where code [email protected]
  select @yingfen =yingfen from score where code [email protected]
  declare @count int--定義標記變數
  set @count=0 --標記變數在下面需要先使用再賦值,所以先給它為0
if @yufen>=60 --判斷語數英是否及格
  set @[email protected]+1--及格的時候count+1
if @shufen>=60
  set @[email protected]+1
if @yingfen>=60
  set @[email protected]+1

if @count=3 --判斷count的值:判斷幾門課及格
  print‘優秀‘
else if @count=2
  print‘結業‘
else if @count=1
  print‘不結業‘
else
  print‘請重修‘
go
--執行
  exec sevenproc 4

4.不帶參數帶傳回值的預存程序
create proc elevenproc
as
return 5
go
--執行
--需要一個變數來接收這個傳回值
declare @fan int
exec @fan=elevenproc
print @fan

5.帶參數,帶傳回值的預存程序
create proc twelveproc
@one int,
@two int
as
declare @sum int
set @[email protected][email protected]
return @sum
go
--執行
declare @fanhuizonghe int
exec @fanhuizonghe = twelveproc 2,4
print @fanhuizonghe

練習:輸入一個學生的學號,想要經過預存程序之後得到在這個學生的總分
create proc thirteenproc
@code int
as
  declare @yu decimal(18,2),@shu decimal(18,2),@ying decimal(18,2)
  select @yu =yufen from score where code [email protected]
  select @shu =shufen from score where code [email protected]
  select @ying =yingfen from score where code [email protected]
  declare @sum int
  select @[email protected][email protected][email protected]
  return @sum
go

  declare @fan int
  exec @fan =thirteenproc 5
  print @fan

 

SQL server 變數if,while,預存程序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.