0% found this document useful (0 votes)
39 views26 pages

Transact-SQL: Mardhiya Hayaty, ST, M.Kom

Transact-SQL (T-SQL) is the procedural language extension for SQL Server. T-SQL includes control-of-flow language elements like IF/ELSE statements and WHILE loops. Variables are declared using DECLARE and assigned values with SET or SELECT. Cursors allow iterating through result sets row-by-row. T-SQL also provides global variables prefixed with @@ that provide information about the SQL Server instance. Control-of-flow elements like IF/ELSE, WHILE, and CASE statements allow conditional and iterative execution of Transact-SQL code.

Uploaded by

Viski Novitasari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views26 pages

Transact-SQL: Mardhiya Hayaty, ST, M.Kom

Transact-SQL (T-SQL) is the procedural language extension for SQL Server. T-SQL includes control-of-flow language elements like IF/ELSE statements and WHILE loops. Variables are declared using DECLARE and assigned values with SET or SELECT. Cursors allow iterating through result sets row-by-row. T-SQL also provides global variables prefixed with @@ that provide information about the SQL Server instance. Control-of-flow elements like IF/ELSE, WHILE, and CASE statements allow conditional and iterative execution of Transact-SQL code.

Uploaded by

Viski Novitasari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Transact-SQL

Mardhiya Hayaty, ST, M.Kom


SQL
● SQL is a standart languange from accessing and manipulating database

● SQL was initially developed at IBM by Donald D. Chamberlin and


Raymond F. Boyce in the early 1970s.This version, initially called SEQUEL
(Structured English QUEry Language)

● SQL became a standard of the American National Standards Institute


(ANSI) in 1986, and of the International Organization for Standardization
(ISO) in 1987.

● Many vendors use SQL as standardization (ORACLE, SQL


Server,PostgreSQL,sybase,informix, postgreSQL)
SQL
● PL/SQL is a procedural language used by Oracle

● pgSQL is a procedural language used by PostgreSQL

● TSQL is a procedural language used by Microsoft in SQL Server.


Types of SQL
􀁺 Retrieving Data with Select Statement
􀁺 DDL (Data Definition Languages)
􀁺 DML (Data Manipulation Languages)
􀁺 DTL (Data Transaction Languages)
􀁺 DCL (Data Control Languages)
T-SQL
Transact-SQL is central to using Microsoft SQL Server. All applications that
communicate with an instance of SQL Server do so by sending Transact-SQL
statements to the server, regardless of the user interface of the application.
T-SQL Framework
● TRANSACT-SQL begins with the declaration of variables and followed by
Block Program.
Declaration of variable
● Transact-SQL provides the following statements to declare and set local
variables: DECLARE, SET and SELECT.

● Variables are declared in the body of a batch or procedure with the


DECLARE statement and are assigned values by using either a SET or
SELECT statement.

● After declaration, all variables are initialized as NULL, unless a value is


provided as part of the declaration.
Declaration of variable
Example :

--declaration of variable
DECLARE @v1 int
DECLARE @nama varchar(30)
-- give a value of 100 to the variable “v1”
select @v1=100
-- give a value of “shofia” to the variable “nama”
select @nama=’Shofia’
Declaration of variable
● SELECT is used also for the execution of arithmetic or other.
Example:
select @v1 = @v1 – 100
select @tgl_skrg = now
select @tahun_skrg = year(now)

● as a parameter in a query
*) in sqlserver2000 version, northwind database
Example:
DECLARE @find varchar(30);
SET @find = 'nancy';
SELECT firstname,title
FROM employees
WHERE firstname=@find
Declaration of variable
● Take the value of a query

DECLARE @find varchar(30)


DECLARE @namadepan varchar(30)
DECLARE @title varchar(30)
SET @find = 'nancy'
--the query
SELECT @namadepan=firstname,@title=title
FROM Employees
WHERE firstname=@find

--PRINT is the function that displays text and variable on the console (screen).
print @namadepan +' -- '+@title
the contents of employees table if you want to save the data into a variable
*) in sqlserver2000 version, northwind database
and every "record/row" will be displayed on
the screen, what should be done ?
WE use
CURSOR DATA TYPE

About CURSOR DATA TYPE , you can get the material from :

https://msdn.microsoft.com/en-us/library/ms181441.aspx
Declare Cursor
--declaration of variable
DECLARE @namadepan varchar(30)
DECLARE @title varchar(30)
--declaration cursor data type, the name is employees_data
declare employees_data cursor for
SELECT firstname,title FROM Employees

--open the cursor


open employees_data
--the position of cursor on the first record
FETCH NEXT FROM employees_data
INTO @namadepan, @title
Declare Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
--PRINT is the function that displays text and variable on the
console (screen).
print @namadepan +' -- '+@title
--for each records/rows from employees table will be saved
into "@namadepan" and "@title" variable
FETCH NEXT FROM employees_data
INTO @namadepan, @title
END
--close the employees_data cursor
CLOSE employees_data
DEALLOCATE employees_data
Global Variable
● SQL Server provides a massive number
of global variables, which are very
effective to use in regular Transact-
SQL.

● Global variable names begin with a @@


prefix.

● Globar variable is read-only.You do not


need to declare them, since the server
constantly maintains them.
Global Variable
print 'Jumlah Koneksi :'+ cast( @@connections as varchar(10) )

print 'Bahasa :'+ @@language

print 'Maksimum Koneksi :'+cast(@@max_connections as varchar(5)


)

print 'Nama Server:'+ @@servername

print 'Versi SQL Srver:'+ @@version


Control-of-Flow Language (Transact-SQL)
● IF statement
● While statement….Continue and break
● Case..When
IF statement (T-SQL)
● Imposes conditions on the execution of a Transact-SQL statement. The Transact-SQL statement
(sql_statement) following the Boolean_expression is executed if theBoolean_expression
evaluates to TRUE. The optional ELSE keyword is an alternate Transact-SQL statement that is
executed when Boolean_expression evaluates to FALSE or NULL.
● Syntax

IF Boolean_expression { sql_statement | statement_block }


[ ELSE { sql_statement | statement_block } ]
IF statement (T-SQL)
● example : using a simple boolean expression
DECLARE @Number int
SET @Number = 600
IF @Number > 100
PRINT 'The number is large.'
ELSE
PRINT 'The number is small.'
IF statement (T-SQL)
● example : Using 1 query as pat of boolean expression
*) in sqlserver 2000 version, northwind database

use northwind
IF (SELECT count(*) FROM customers WHERE city LIKE 'mexico%' ) > 5
PRINT 'There are more than 5 customers from mexico'
ELSE
RINT 'There are 5 or less customers from mexico'
While statement (T-SQL)
● Sets a condition for the repeated execution of an SQL statement
or statement block. The statements are executed repeatedly as
long as the specified condition is true. The execution of
statements in the WHILE loop can be controlled from inside the
loop with the BREAK and CONTINUE keywords.
● Syntax :
WHILE Boolean_expression
{ sql_statement | statement_block | BREAK | CONTINUE }
While statement (T-SQL)
example :
declare @i int
select @i=10
while @i > 0
begin
PRINT 'i=' + convert(varchar(5),@i)
select @i= @i-1
End
While statement (T-SQL)
example : while..with break and continue statement.

declare @i int
select @i=10
while @i > 0
begin
PRINT 'i=' + convert(varchar(5),@i)
select @i= @i-1
if @i=4
break
else
continue
End
Case statement (T-SQL)
Syntax:
● Simple CASE expression:

CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END

● Searched CASE expression:

CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Case statement (T-SQL)
example : Simple CASE expression
*) in sqlserver 2000 version, northwind database

use northwind
SELECT lastname,firstname, gender =
CASE titleofcourtesy
WHEN 'mrs.' THEN 'female'
WHEN 'Mr.' THEN 'male'
WHEN 'Ms.' THEN 'male'
eLSE 'no identify'
END,titleofcourtesy
FROM employees
ORDER BY lastname
Case statement (T-SQL)
example : Searched CASE expression
*) in sqlserver 2000 version, northwind database

use northwind
SELECT Productname, "Price Range" =
CASE
WHEN unitPrice = 0 THEN 'item - not for resale'
WHEN unitPrice < 50.000 THEN 'Under $50'
WHEN unitPrice >= 50.000 and unitPrice < 250.000 THEN 'Under $250'
WHEN unitPrice >= 250.000 and unitPrice < 100.0000 THEN 'Under
$1000'
ELSE 'Over $1000'
END
FROM Products

You might also like