You are on page 1of 15

Loops in Sql

IF … ELSE IF and PRINT

• IF statement is pretty simple, right after the IF keyword, you’ll put the
condition. If that condition evaluates, the block of statements shall
execute. If there is nothing else, that’s it.
• You could also add ELSE to the IF statement, and this will result in the
following – if the original condition wasn’t true, the code in the ELSE
part should execute.
• If we want to test multiple conditions, we’ll use, IF (1st condition) …
ELSE IF (2nd condition) … ELSE IF (n-th condition) … ELSE. We’ll do
exactly that in our example – just to show how it works in SQL Server.
• But before that – the PRINT command. PRINT simply prints the text
placed after that command. That is inside quotes, but you could also
concatenate strings and use variables.
• DECLARE @num1 INTEGER;
• DECLARE @num2 INTEGER;
•  
• SET @num1 = 20;
• SET @num2 = 30;
•  
• IF (@num1 > @num2)
•   PRINT '1st number is greater than 2nd number.'
• ELSE IF (@num2 > @num1)
•   PRINT '2nd number is greater than 1st number.'
• ELSE
•   PRINT 'The numbers are equal.';
DECLARE @num1 INTEGER;
DECLARE @num2 INTEGER;
 
SET @num1 = 100;
SET @num2 = 30;
 
IF (@num1 > @num2) BEGIN
  PRINT '1st number is greater than 2nd number.'
  IF (@num1 > 75)
    PRINT '1st number is greater than 75.'
  ELSE IF (@num1 > 50)
    PRINT '1st number is greater than 50.'
  ELSE
    PRINT '1st number is less than or equal to 50.';
END
ELSE IF (@num2 > @num1)
  PRINT '2nd number is greater than 1st number.'
ELSE
  PRINT 'The numbers are equal.';
WHILE 
• DECLARE @i INTEGER;
• SET @i = 1;
•  
• WHILE @i <= 10
• BEGIN
•    PRINT ( @i);
•    SET @i = @i + 1;
• END;
• A subprogram is a program unit/module that
performs a particular task. These subprograms
are combined to form larger programs. This is
basically called the 'Modular design'. A
subprogram can be invoked by another
subprogram or program which is called
the calling program.
• PL/SQL subprograms are named PL/SQL blocks
that can be invoked with a set of parameters.
PL/SQL provides two kinds of subprograms −
• Functions − These subprograms return a single
value; mainly used to compute and return a
value.
• Procedures − These subprograms do not return
a value directly; mainly used to perform an
action.
• A procedure is created with the CREATE OR
REPLACE PROCEDURE statement. The
simplified syntax for the CREATE OR REPLACE
PROCEDURE statement is as follows −
ROCEDURE procedure_name [(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS} BEGIN < procedure_body >

• CREATE OR REPLACE PROCEDURE greetings


• AS BEGIN
• dbms_output.put_line('Hello World!');
• END;
Drop Procedure
• DROP PROCEDURE greetings;
Stored Procedure With One Parameter

• CREATE PROCEDURE SelectAll @name


nvarchar(30)
• AS
• SELECT * FROM CUSTOMERS1 WHERE Name =
@name
• ;
Stored Procedure With Multiple Parameters

• CREATE PROCEDURE SelectAllCustomers @Cit
y nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City =
@City AND PostalCode = @PostalCode
GO;
• EXEC SelectAllCustomers @City = 'London',
@PostalCode = 'WA1 1DP';

You might also like