Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Basics of T-SQL programming

Declaring variables and Parameters


Cont…
In SQL Server, a variable is typical known as a
local variable is only available in the batch,
stored procedure or code block in which it is
defined.
A local variable is defined using the
DECLARE statement.
◦ The name of the local variable needs to start with
the @ symbol as the first character of its name.
example: DECLARE @Count INT
DECLARE @Count INT, @X INT, @Y INT,
@Z CHAR (10)
Cont…
 A localvariable is initially assigned a NULL value.
A value can be assigned to a local variable by using the
SET or SELECT statement.
Using SET:-
DECLARE @Count INT
SET @Count = 1
Using SELECT statement to initialize the value of a
local variable with the value returned from a select that
fetch the total number of rows in the employee table.
DECLARE @ROWCNT INT
SELECT @ROWCNT= (SELECT COUNT (*) FROM
employee)
Cont…
An example that returns all the Customers
records in the Library database where the
Student’s dept column is equal to ‘CS’
Declare @d varchar(25)
set @d= ‘CS’
select fname from Library.dbo.Student where
dept= @d
IF ... ELSE statemens
T-SQL has the IF statement to help with
allowing different code to be executed based
on the results of a condition.
Format one:
IF <condition> <then code to be executed >
Format two:
IF <condition> <then code to be executed >    
ELSE < else code to be executed >.
Ifa block of code is used then it will need to
be enclosed in a BEGIN and END statement.
Example:
Declare @x int
set @x = 29
if @x = 29 print 'The number is 29'
if @x = 30 print 'The number is 30'
Now the condition statement can also
contain a SELECT statement.
The SELECT statement will need to
return value or set of values that can be
tested.
Cont…
If a SELECT statement is used the
statement needs to be enclosed in
parentheses.
if (select count(*) from Student
where fname like '[A-D]%') > 0
print 'A-D Students are Found '
Cont…
if db_name() = 'master'
begin
Print 'We are in the Master Database'
Print ‘ ’
Print 'So whatever code you execute must be done carefully'
End
else if db_name() = ‘Library'
begin
Print 'We are in the test Library database ’
Print 'So Enjoy'
End
Cont…
Suppose we want to determine whether to
update or add a record to the employee
table in the Library database.
if exists(select * from Student where
Studid = '123')
Print ‘Need to update’
else
Print 'Need to add new rcord'
WHILE Loop With CONTINUE and
BREAK Keywords
Syntax:
WHILE Boolean_expression
{ sql_statement | statement_block | BREAK | CONTINUE }
{sql_statement | statement_block}
BREAK: Causes an exit from the
innermost WHILE loop.
CONTINUE: Causes the WHILE loop to
restart, ignoring any statements after the
CONTINUE keyword.
Example 1:

DECLARE @intFlag INT


SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
END
Example 2: Usage of WHILE Loop with
BREAK keyword
DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
IF @intFlag = 4
BREAK;
END
GO
Example 3
WHILE (SELECT AVG(ListPrice) FROM
Production.Product) < 300
BEGIN
UPDATE Production.Product
SET ListPrice = ListPrice * 2
SELECT MAX(ListPrice) FROM Production.Product
IF (SELECT MAX(ListPrice) FROM Production.Product) > 500
BREAK
ELSE
CONTINUE
END
In the following example, if the average list
price of a product is less than $300,
the WHILE loop doubles the prices and then
selects the maximum price.
If the maximum price is less than or equal
to $500, the WHILE loop restarts and doubles
the prices again.
This loop continues doubling the prices until
the maximum price is greater than $500, and
then exits the WHILE loop.
Example 4
Select EID, salary into #t from Employee
declare @i int,@count int
set @i=1
select @count=COUNT(EID) from #t
while(@i<=@count)
begin
update Employee
set Salary=Salary*1.5
where Employee.EID=(select top 1 EID from #t)
Delete #t
where #t.EID=(select top 1 EID from #t)
set @i=@i+1
end
CASE… When expressions
Assignment 1
1. Using if …Else statement write a T-SQL
program to print the grade of a student.
Assume that a student table has (StudId,
FN,LN, Age, mark) and write A if the
mark is above 80, B if the mark is between
79 and 60 and C if is less than 60
2. Use while… loop write a T-SQL program
to find the factorial of a number
3. Describe the Syntax of CASE……When .
Illustrate with example

You might also like