SQL 1
SQL 1
SQL 1
Procedural language support PL/SQL is a development tools not only for data
manipulation futures but also provide the conditional checking, looping or
branching operations same as like other programming language.
Error handling PL/SQL is dealing with error handling, It's permits the smart
way handling the errors and giving user friendly error messages, when the
errors are encountered.
Declare variable PL/SQL gives you control to declare variables and access
them within the block. The declared variables can be used at the time of query
processing.
PL/SQL have a great functionality to display multiple records from the multiple
tables at the same time.
1. IF Statement
IF condition
Statement;
1
DECLARE
@a int = 200;
BEGIN
-- check the boolean condition using if statement
IF( @a < 10 )
-- if condition is true then print the following
print('a is less than 10 ');
print('value of a is : ' + CAST(@a AS varchar));
END;
2. IF-THEN-ELSE Statement
IF condition
[Statements to execute when condition is TRUE]
ELSE
[Statements to execute when condition is FALSE]
DECLARE
@a int = 200;
BEGIN
-- check the boolean condition using if statement
IF( @a < 10 )
-- if condition is true then print the following
print('a is less than 10 ');
ELSE
print('a is not less than 10 ');
print('value of a is : ' + CAST(@a AS varchar));
END;
3. IF-THEN-ELS-IF-ELSE Statement
IF condition1
Statements to execute when condition1 is TRUE
ELSE IF condition2
Statements to execute when condition2 is TRUE
ELSE
Statements to execute when both condition1 and condition2 are FALSE
END;
DECLARE
@a int = 200;
BEGIN
-- check the boolean condition using if statement
IF( @a < 10 )
-- if condition is true then print the following
print('a is less than 10 ');
2
ELSE IF (@a > 10)
print('a is greater than 10 ');
ELSE
print('a is zero');
print('value of a is : ' + CAST(@a AS varchar));
END;
Iterative statements
While Loop
WHILE <condition>
LOOP statements;
END;
SET @count = 1;
WHILE @count<= 10
BEGIN
PRINT @count
END;
Scalar Function: It is a function that return single value. Generally, we have to define the
function body between BEGIN … END block. We can use any SQL data type as the return
type except text, image, ntext, cursor, and timestamp.
3
The basic syntax behind the SQL Server User defined functions is as shown below:
RETURN Data
END
Return_Type:
Data Type: Please specify the data type of return value. For example, VARCHAR, INT, FLOAT
etc.
Data: Please specify the return value, and it should match the Data Type. This can be single
value or Table
Function_Name: You can specify any name you wish to give other than the system reserved
keywords. Please try to use meaningful names so that you can identify them easily.
@Parameter_Name: Every function accepts zero or more parameters, it’s completely depends
upon the user requirements. While declaring the parameters don’t forget the appropriate data
type. For example (@name VARCHAR(50), @number INT)
Function Body: Any SQL query, or any complex mathematical calculations you want to implement
in this particular function.
Scalar-Valued functions
4
Example:1
CREATE FUNCTION average_salary()
RETURNS int
AS
BEGIN
RETURN (SELECT avg(salary) FROM emptable)
END
Example:2
5
Table-Valued functions