Transact-SQL: Mardhiya Hayaty, ST, M.Kom
Transact-SQL: Mardhiya Hayaty, ST, M.Kom
--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
--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
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
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