Trong phần 1, phần 2 và phần 3 của loạt bài này, chúng tôi đã giới thiệu cách sử dụng các hàm CASE đơn giản trong truy vấn. Trong phần tiếp theo này, tôi sẽ giải thích cách sử dụng các hàm CASE trong mệnh đề như ‘Group by’ set quoted_identifier off go use tempdb go if exists (select * from dbo.sysobjects where id = object_id(N'[emp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [emp] GO create table Emp (id int, [First name] varchar(50), [Last name] varchar(50), Salary money, state char(2)) go insert into Emp (id,[First name],[Last name], salary, State ) values (1,'John','Smith',120000,'WA') insert into Emp (id,[First name],[Last name], salary, State ) values (2,'James','Bond',95000,'OR') insert into Emp (id,[First name],[Last name], salary , State) values (3,'Alexa','Mantena',200000,'WY') insert into Emp (id,[First name],[Last name], salary, State ) values (4,'Shui','Qui',36000,'CO') insert into Emp (id,[First name],[Last name], salary, State ) values (5,'William','Hsu',39000,'NE') insert into Emp (id,[First name],[Last name], salary , State) values (6,'Danielle','Stewart',50000,'TX') insert into Emp (id,[First name],[Last name], salary , State) values (7,'Martha','Mcgrath',400000,'PA') insert into Emp (id,[First name],[Last name], salary, State ) values (8,'Henry','Fayol',75000,'NJ') insert into Emp (id,[First name],[Last name], salary, State ) values (9,'Dick','Watson',91000,'NY') insert into Emp (id,[First name],[Last name], salary, State ) values (10,'Helen','Foster',124000,'AK') go Và bây giờ chúng ta cần có 6 bảng để lưu trữ các ID của nhân viên thuộc các vùng thời gian khác nhau như trình bày sau if exists (select * from dbo.sysobjects where id = object_id(N'[eastern]') and objectproperty(id, N'isusertable') = 1) drop table [eastern] go create table eastern (id int) if exists (select * from dbo.sysobjects where id = object_id(N'[mountain]') and objectproperty(id, N'isusertable') = 1) drop table [mountain] go create table mountain (id int) if exists (select * from dbo.sysobjects where id = object_id(N'[hawaii]') and objectproperty(id, N'isusertable') = 1) drop table [hawaii] go create table hawaii (id int) if exists (select * from dbo.sysobjects where id = object_id(N'[central]') and objectproperty(id, N'isusertable') = 1) drop table [central] go create table central (id int) if exists (select * from dbo.sysobjects where id = object_id(N'[alaskan]') and objectproperty(id, N'isusertable') = 1) drop table [alaskan] go create table alaskan (id int) if exists (select * from dbo.sysobjects where id = object_id(N'[pacific]') and objectproperty(id, N'isusertable') = 1) drop table [pacific] go create table pacific (id int) go insert into pacific (id) values (1) insert into pacific (id) values (2) insert into mountain (id) values (3) insert into mountain (id) values (4) insert into central (id) values (5) insert into central (id) values (6) insert into eastern (id) values (7) insert into eastern (id) values (8) insert into eastern (id) values (9) insert into alaskan (id) values (10) go Nếu bạn muốn biết toàn bộ nhân viên thuộc vùng thời gian ở Eastern, bạn chắc chắn sẽ phải thực thi một câu lệnh truy vấn đơn giản như dưới đây
Câu lệnh truy vấn trên sẽ trả về kết quả như sau
Vậy bây giờ giả sử chúng ta cần tạo một kịch bản cho phép đưa một khu vực thời gian vào trong một biến và hiển thị ra kết quả dựa trên giá trị của biến đó. Điều này hoàn toàn có thể làm được khi sử dụng mệnh đề và hàm CASE như sau:
Đoạn kịch bản trên sẽ có kết quả như sau: id First name Last name salary state TimZone ---------------------------------------------------------------- 1 John Smith 120000.00 WA Pacific 2 James Bond 95000.00 OR Pacific Đoạn script trên có thể được viết trong một thủ tục như sau:
Và thực thi hàm đã tạo ở trên bằng câu lệnh
Hàm sẽ đưa ra kết quả:
Kết luận ![]() Topics: Công nghệ mới |