SQL Process Control Statements

Source: Internet
Author: User

If-else keywords

IF (condition)

Statement or statement block

ELSE

Statement or statement block

Statement or statement block with begin ... End indicates

IF (condition)

BEGIN

Statement 1

Statement 2

END

For example:

DECLARE @test INT

SET @test =1

SELECT @test =avg (score) from Stu WHERE stu.name= ' Lilu '

IF (@test =1)

PRINT @test

ELSE

BEGIN

IF exists (SELECT score from Stu WHERE stu.name= ' AA ')

BEGIN

DELETE from Stu WHERE stu.name= ' AA ' and score=99

END

INSERT into Stu VALUES (' AA ', 99)

SELECT score from Stu

WHERE name= ' AA '

END
650) this.width=650; "title=" 1452692354903794625.jpg "src=" https://s1.51cto.com/wyfs02/M00/06/97/ Wkiom1m6guiity0naadvzlyyswq316.jpg-wh_500x0-wm_3-wmp_4-s_1208296753.jpg "alt=" Wkiom1m6guiity0naadvzlyyswq316.jpg-wh_50 "/>

Case keyword

Case

Case

When condition 1 then result 1

When condition 2 then result 2

END

For example:

CREATE TABLE SS

(

Name varchar (10),

Score Float

)

INSERT into SS values (' Small white ', 99)

INSERT into SS values (' Little black ', 77)

INSERT into SS values (' Xiao Li ', 58)

SELECT name,

Score=case

When score<60 then ' E '

When score between, and then ' D '

When score between and "C"

When score between and the then ' B '

ElSE ' A '

END

from SS

650) this.width=650; "title=" 1955969613262554745.jpg "src=" https://s4.51cto.com/wyfs02/M00/A5/48/ Wkiol1m6gtqcll-zaadjpoxkwpg605.jpg-wh_500x0-wm_3-wmp_4-s_1318730562.jpg "alt=" Wkiol1m6gtqcll-zaadjpoxkwpg605.jpg-wh_50 "/>

While keyword

while (condition)

Statement or statement block

[Breake]

DECLARE @test INT

SET @test =2

while (@test <20)

BEGIN

SET @[email protected]+2

IF (@test <>7)

BEGIN

PRINT @test

IF (@test =18)

Break

END

ELSE

IF (@test =7)

CONTINUE

END

650) this.width=650; "title=" 1925007365824330023.jpg "src=" https://s5.51cto.com/wyfs02/M02/A5/48/ Wkiol1m6gvjy98lwaac16oprxcy229.jpg-wh_500x0-wm_3-wmp_4-s_172332444.jpg "alt=" Wkiol1m6gvjy98lwaac16oprxcy229.jpg-wh_50 "/>

This article is from the "Linux_woniu" blog, make sure to keep this source http://llu1314.blog.51cto.com/5925801/1965331

SQL Process Control Statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.