PE 459 LECTURE 2- natural gas basic concepts and properties
Advanced DB lab 1 (2).pptx
1. Basics of T-SQL programming
Declaring variables and Parameters
1
2. • 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
• More than one variable can be defined with a single DECLARE
statement with a comma
Example: DECLARE @Count INT, @X INT, @Y INT, @Z
• A local variable 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 customer table.
declare @rowcount int
select @rowcount=(selectcount(*)from customer
print @rowcount
2
3. • An example that returns all the Customers records in the sales
database where the customer’s address column is equal to ‘Bahir
Dar’
declare @myaddress varchar(50)
set @myaddress='bahir dar'
select*from customer where [address]=@myaddress
Example
declare @var1 int,@var2 float,@var3 int,@sum int
set @var1=10
set @var2=20
set @var3=30
set @sum=@var1+@var2+@var3
print @sum
N.B.you can also use select to assign and to
display
3
4. IF ... ELSE statement
• 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 >.
• If a block of code is used then it will need to be enclosed in a BEGIN
and END statement.
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. 4
5. Example
if(selectcount(*)from customer)<20
print'Number of customer is less than 20’
• If a SELECT statement is used the statement needs to be enclosed in parentheses.
parentheses.
Example
if (select count(*) from Student
where fname like '[A-D]%') > 0
print 'A-D Students are Found ‘
Example
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' 5
6. •Suppose we want to determine whether to update or
add a record to the Customer table in the Sales
database.
if exists(select * from customer where
Customer_ID = 'C-150')
Print 'Need to update'
else
Print 'Need to add new record'
Example
if exists(select*from customer
where customer_id='c-100')
print'record is found'
else
print'record is not found'
6
7. Example
declare @x int
set @x=-80;
if(@x>0)
begin
print'number is positive'
end
else if(@x<0)
begin
print'number is negative'
end
else
print'number is zero'
7
8. 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
DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
END
8
9. Example : 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
declare @start int
set @start=65
while(@start<=90)
begin
print char(@start)
set @start+=1
end 9
10. Example
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 above example, if the average list price of a product
is less than $300, the WHILE loop doubles the prices
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. 10
11. CASE… When expressions
•Evaluates a list of conditions and returns one of multiple
possible result expressions.
•The CASE expression has two formats:
- The simple CASE expression compares an expression to
a set of simple expressions to determine the result.
- The searched CASE expression evaluates a set of
Boolean expressions to determine the result. Within a
SELECT statement, the searched CASE expression
allows for values to be replaced in the result set based on
comparison values.
Syntax for The simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n
]
[ELSE else_result_expression] 11
12. Example
declare @x int
set @x=20
select
case
when @x>0 then'x is positive'
when @x<0 then'x is negative'
else'x is zero'
endas'My result 1’
Example
declare @my_variable int
set @my_variable=2
select
case @my_variable
when 1 then'the number is one'
when 2 then'the number is two'
else'the number is unknown'
endas'My result 2’
Example
select first_name,
case [address]
when'bahir dar'then'your address is bahir dar'
when'Addis Abeba'then'your address is addis'
when'Gonder'then'your address is gondar'
else'address unknown'
endas'your address'
from customer
orderby first_name
12
13. Example
select first_name,last_name,
case sex
when'm'then'you are male'
when'f'then'you are female'
else'unknown'
endas'Gender'
from customer
orderby first_name
Example
select first_name,last_name,
case
when sex='m'then'you are male'
when sex='f'then'you are female'
else'unknown'
endas'Gender'
from customer
orderby first_name
13
14. Exercise
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……When14
15. Use the following sample database for your example
create database sales
use sales
create table customer
(
Customer_ID varchar(50)not null,
First_Name varchar(200)not null,
Last_Name varchar(200) not null,
Sex char(1),
[Address] varchar(255),
primary key(Customer_ID)
)
create table Product
(
Product_ID char(200),
Product_Name varchar(255),
purchase_Price money,
Acquired_Date date,
product_price money,
Manufacturer varchar(255),
primary key(Product_ID)
)
create table [Order]
(
Order_ID int identity(1,1),
Product_ID char(200),
Customer_ID varchar(50),
Order_Date date,
primary key(Order_ID),
foreign key(Product_ID)references
Product(Product_ID),
foreign key(Customer_ID)references
customer(Customer_ID)
)
15