Download presentation
Presentation is loading. Please wait.
Published byAna McKnight Modified over 9 years ago
1
Transact-SQL
2
1. Declare declare @discount float set @discount = 10 select * from customers where discnt > @discount
3
2. Get values from the select result declare @mindiscount float select @mindiscount = min(discnt) from customers
4
3. Display the result (in 2, the output is disabled, nothing is displayed) select @mindiscount as DISCOUNT
5
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 customers select @@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'
6
5. Begin... End / If... Else / Continue / Break declare @v1 int, @v2 int set @v1 = 0 set @v2 = 100 while (@v1 < 10) begin if(@v2 % 2 = 0) set @v2 = @v2 + 1 else set @v2 = @v2 + 2 set @v1 = @v1 + 1 end select @v1 as v1, @v2 as v2
7
5. Begin... End / If... Else / Continue / Break (cont) declare @v1 int, @v2 int set @v1 = 0 set @v2 = 100 while (@v1 < 10) begin if(@v1 = 5) break else set @v2 = @v2 + 2 set @v1 = @v1 + 1 end select @v1 as v1, @v2 as v2
8
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
9
7. Print declare @mystr varchar(32) set @mystr = 'Hello' print @mystr //Also try select @mystr
10
8. Go Signals the end of a batch of Transact-SQL statements to the Microsoft® SQL Server™ utilities declare @mindiscount float select @mindiscount = min(discnt) from customers select @mindiscount GO select @mindiscount //@mindiscount is not defined after 'GO'
11
9. Operators +, -, *, /, % =,, =, !=, !>, !<
12
10. Data types int, smalling, char(n), varchar(n), money, float
13
11.1 Stored procedure - create 1. create proc get_customer @cid char(4) as select cname from customers where cid = @cid go 2. Exec get_customer SELECT CID FROM CUSTOMERS ’
14
11.2 help/delete 1. check the interface sp_help get_customer 2. check the source code sp_helptext get_customer 3. delete drop proc get_customer
15
11.3 different return results create proc myproc1 as declare @v1 int set @v1 = 1 return create proc myproc2 as declare @v1 int set @v1 = 1 select @v1 return
16
11.4 return multiple datasets create proc myproc3 as select * from customers select * from agents return
17
11.5 multiple input parameters create proc myproc4 @v1 float, @v2 int as select * from customers where discnt < @v1 select * from agents where percentage < @v2 return exec myproc4 15,5
18
11.6 return values create proc myproc5 @discount float, @counter int output as select @counter = count(*) from customers where discnt < @discount return declare @mycounter int exec myproc5 15, @mycounter output select @mycounter
19
11.6 return values - cont create proc myproc6 @discount float, @counter int output as select * from customers where discnt < @discount set @counter = @@rowcount return declare @mycounter int exec myproc6 15, @mycounter output select @mycounter
20
11.6 return values - cont create proc myproc7 @counter1 int output, @counter2 int output as select * from customers set @counter1 = @@rowcount select * from agents set @counter2 = @@rowcount return declare @mycounter1 int, @mycounter2 int exec myproc7 @mycounter1 output, @mycounter2 output select @mycounter1, @mycounter2
21
12. UDF (user-defined funcation) create function average_dollars(@customerid char(4)) returns money as begin declare @avg money select @avg = avg(dollars) from orders where cid = @customerid return @avg end sp_help average_dollars sp_helptext average_dollars
22
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.