22
Transact-SQL

Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

Embed Size (px)

Citation preview

Page 1: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

Transact-SQL

Page 2: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

1. Declare

declare @discount float

set @discount = 10

select * from customers where discnt > @discount

Page 3: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

2. Get values from the select result

declare @mindiscount float

select @mindiscount = min(discnt) from customers

Page 4: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

3. Display the result (in 2, the output is disabled, nothing is displayed)

select @mindiscount as DISCOUNT

Page 5: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

4. @@ROWCOUNT

Returns the number of rows affected by the last statement.

(When you do the programming, you need to know there are how many records in the query resutls)

select * from customersselect @@rowcount as RowCounter

insert customers values ('c019', 'Heller', 'Rebecca', 12)select @@rowcount as RowCounter

update customers set name = 'Jones'where cid = 'c020'If @@ROWCOUNT = 0 print 'Warning: No rows were updated'

Page 6: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

5. Begin ... End / If ... Else / Continue / Break

declare @v1 int, @v2 intset @v1 = 0set @v2 = 100

while (@v1 < 10)begin if(@v2 % 2 = 0) set @v2 = @v2 + 1 else set @v2 = @v2 + 2 set @v1 = @v1 + 1end

select @v1 as v1, @v2 as v2

Page 7: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

5. Begin ... End / If ... Else / Continue / Break (cont)

declare @v1 int, @v2 intset @v1 = 0set @v2 = 100

while (@v1 < 10)begin if(@v1 = 5) break else set @v2 = @v2 + 2 set @v1 = @v1 + 1end

select @v1 as v1, @v2 as v2

Page 8: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

6. Case (alias)

select cname, category = case

when discnt < 8 then 'low'

when discnt between 8 and 10 then 'med'

else 'high'

end

from customers

Page 9: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

7. Printdeclare @mystr varchar(32)

set @mystr = 'Hello'

print @mystr

//Also try select @mystr

Page 10: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

8. GoSignals the end of a batch of Transact-SQL

statements to the Microsoft® SQL Server™ utilities

declare @mindiscount floatselect @mindiscount = min(discnt) from customersselect @mindiscount GOselect @mindiscount //@mindiscount is not

defined after 'GO'

Page 11: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

9. Operators• +, - , *, /, %

• =, < , > , <=, >=, !=, !>, !<

Page 12: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

10. Data types• int, smalling, char(n), varchar(n), money,

float

Page 13: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.1 Stored procedure - create1. create proc get_customer @cid char(4)as select cname from customers where cid = @cidgo

2. Exec get_customer SELECT CID FROM CUSTOMERS ’

Page 14: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.2 help/delete1. check the interfacesp_help get_customer

2. check the source codesp_helptext get_customer

3. delete drop proc get_customer

Page 15: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.3 different return resultscreate proc myproc1as declare @v1 int set @v1 = 1return

create proc myproc2as declare @v1 int set @v1 = 1 select @v1return

Page 16: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.4 return multiple datasets

create proc myproc3

as

select * from customers

select * from agents

return

Page 17: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.5 multiple input parameters

create proc myproc4@v1 float,@v2 intas select * from customers where discnt < @v1 select * from agents where percentage < @v2return

exec myproc4 15,5

Page 18: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.6 return values

create proc myproc5@discount float,@counter int outputas select @counter = count(*) from customers where discnt < @discountreturn

declare @mycounter intexec myproc5 15, @mycounter outputselect @mycounter

Page 19: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.6 return values - cont

create proc myproc6@discount float,@counter int outputas select * from customers where discnt < @discount set @counter = @@rowcountreturn

declare @mycounter intexec myproc6 15, @mycounter outputselect @mycounter

Page 20: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

11.6 return values - contcreate proc myproc7@counter1 int output,@counter2 int outputas select * from customers set @counter1 = @@rowcount select * from agents set @counter2 = @@rowcountreturn

declare @mycounter1 int, @mycounter2 intexec myproc7 @mycounter1 output, @mycounter2 outputselect @mycounter1, @mycounter2

Page 21: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

12. UDF (user-defined funcation)

create function average_dollars(@customerid char(4))returns moneyasbegin declare @avg money select @avg = avg(dollars) from orders where cid = @customerid return @avgend

sp_help average_dollars

sp_helptext average_dollars

Page 22: Transact-SQL. 1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount

12. UDF - cont

declare @avg_sale float

set @avg_sale = zhoupf.average_dollars('c001')

select @avg_sale

*Inside UDF, don’t make any changes to tables.

*Difference between UDF and stored procedure.