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