Faculty of Computing and Informatics
DPG 621S
V. Paduri email: [email protected]
Lecture Notes: Control Structure and Parameters
1
SQL SERVER
Conditional Statements
While loop: In SQL SERVER, while loop can be
used in similar manner as any other programming
language. A while loop will check the condition
first and then execute the block of SQL
Statements within it as long as the condition
evaluates true.
2
SQL SERVER
Syntax:
WHILE condition
BEGIN
{...statements...}
END;
3
SQL SERVER
Parameters:
1. Condition: The condition is tested in each pass
through the loop. If condition evaluates to TRUE,
the loop body is executed otherwise the loop is
terminated.
2. Statements: The statements that needs to be
executed in each pass through the loop.
4
SQL SERVER
5
SQL SERVER
Output:
6
SQL SERVER
• Break statement: BREAK statement as the
name signifies is used to break the flow of
control. It can be used in SQL in similar
manner as any other programming
language.
• Example: While loop with Break
statement
7
SQL SERVER |
8
SQL SERVER
Output
9
SQL SERVER
Example
• Note : In the example, when variables value
became five, BREAK Statement is executed
and the control gets out from the Loop.
• Do-While loop: SQL server does not have
the feature of do-while loop but by doing
little modifications in while loop, the same
behaviour can be achieved.
10
SQL SERVER
Example
11
SQL SERVER
Output
12
SQL SERVER
Example 2:
13
SQL SERVER
Output 2:
14
SQL SERVER
• CASE statement: In SQL Server, the
CASE statement has the same functionality
as IF-THEN-ELSE statement.
15
SQL SERVER
• Syntax:
• CASE Expression
• WHEN Con_1 THEN Output1
• WHEN Con_2 THEN Output2
• WHEN Con_3 THEN Output3
• WHEN Con_4 THEN Output4
• ...
• WHEN Con_n THEN Outputn
• ELSE output
• END
16
SQL SERVER
• Paramaters:
1. Expression: The value to be compared to the list
of conditions(Optional).
2. Con_1, Con_2, …Con_n: The conditions are
required and are evaluated in the order they are
listed. Once a condition is true, the CASE function
will return the result and not evaluate the conditions
any further.
3. Output1, Output2, …Outputn: The output to be
printed once the condition evaluates true.
17
SQL SERVER
Example |
18
SQL SERVER
Output
19
SQL Server Mathematical
functions
SQL Server Mathematical functions (SQRT, PI,
SQUARE, ROUND, CEILING & FLOOR)
Mathematical functions are present in SQL server
which can be used to perform mathematical
calculations. Some commonly used mathematical
functions are given below:
20
SQL Server Mathematical
functions
1. SQRT():
SQRT() function is the most commonly used
function. It takes any numeric values and returns
the square root value of that number.
Syntax:
SELECT SQRT(..value..)
21
SQL Server Mathematical
functions
22
SQL Server Mathematical
functions
• 2. PI(): There are calculations which require
use of pi. Using pi() function, value of PI
can be used anywhere in the query.
• Syntax: SELECT PI()
23
SQL Server Mathematical
functions
Example
24
SQL Server Mathematical
functions
4. ROUND(): ROUND() function is used to round a value to
the nearest specified decimal place.
Syntax:
SELECT ROUND(..value.., number_of_decimal_places)
25
SQL Server Mathematical
functions
26
SQL Server Mathematical
functions
5. CEILING() and FLOOR()
CEILING(): CEILING() function is used to find the
next highest value (integer).
Syntax:
SELECT CEILING(..value..)
FLOOR(): FLOOR() function returns the next lowest
value (integer).
Syntax:
SELECT FLOOR(..value..)
27
SQL Server Mathematical
functions
28