T_sql programming assignment, branching statements, looping

Source: Internet
Author: User

We often use assignments, loops, branching statements, etc. in C #

Today, let's look at what we used in C # to put in SQL.

Creating variables

Creating a value-type variable in C # is simple int A; that's fine.

Sql:declare @a int; -This is the creation of an int type variable a in SQL

Be sure to remember to create a variable in SQL before you must add @

Assign value

Set @a = 123

Now is the assignment of a to a value of 123.

Assign also add @

Output

Output in VS you remember that, Console.write/console.writeline?

That's the output in SQL, which is divided into two types

First Kind

Select @a--this is only mapped to the result set, note that the mapping

The second Kind

Print @a-This is the output in SQL

Branch statements

It's still if..........else.

But slightly changed.

1 Declare @a int2 Declare @b int3 4 Set @a = 1235 Set @b = 4566 7 8 if @a > @b9 beginTen     Select 'wrong' One End A Else - begin -     Select 'the' the End

The output of the time remember that the full selection of SQL will not be the same as vs automatically give you a sentence to execute SQL is you choose which sentence he gave you to execute which sentence

So create variables and assign variables to be selected for execution, or SQL will throw you a contemptuous look at you, else if the same

Cycle

The loops are slightly different.

1  Declare @a int2   3  Set @a = 14   5   while @a <= Ten6  begin7       Select @a8       Select @a = @a + 19       Ten  End

But the loop turns out this way.

If that's the case

1  Declare @a int2   3  Set @a = 14   5   while @a <= Ten6  begin7       8       Select @a+19       Select @aTen        One  End

So that's the infinite loop.

Don't ask me why, I just restarted the SQL

Because I didn't notice where the pause key was.

Yes, look at how much CPU and memory can occupy.

Next again.

T_sql programming assignment, branching statements, looping

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.