SlideShare a Scribd company logo
1 of 17
Trường Đại Học Khoa Học Tự Nhiên 
TPHCM 
Môn: Quản Trị Cơ Sở Dữ Liệu 
NHÓM 4 : Lê Thị Kim Trinh _1263146 
Hồ Tuấn Vũ _1263159 
Nguyễn Văn Tú _1263149 
Trương Đặng Huy Hoài _1263081 
NHÓM 4_CURSOR
Chủ đề 5: CURSOR 
GIỚI THIỆU 
NHÓM 4_CURSOR 
CÁCH SỬ 
DỤNG
GIỚI THIỆU 
ĐỊNH 
NGHĨA 
PHÂN LOẠI 
CÚ PHÁP 
KHAI BÁO 
NHÓM 4_CURSOR
Select * 
From SINHVIEN 
Where GIOITINH = 
n’Nam’ 
ĐỊNH NGHĨA 
 VẤN ĐỀ: 
MSSV HOVATEN NGAYSINH GIOITINH 
001 Nguyễn Lam 21/01/1994 Nam 
002 Đỗ Đình Đan Đan 11/11/1994 Nữ 
003 Trần Trung 02/09/1995 Nam 
004 Lê Huỳnh Ngọc 
Minh 
NHÓM 4_CURSOR 
15/12/1994 Nữ 
MSSV HOVATEN NGAYSINH GIOITINH 
001 Nguyễn Lam 21/01/1994 Nam 
003 Trần Trung 02/09/1995 Nam
 ĐỊNH NGHĨA: 
Cursor (con trỏ) là một đối tượng cơ sở dữ 
liệu được sử dụng để khai báo với từng dòng 
cơ sở dữ liệu. 
NHÓM 4_CURSOR
Con trỏ tĩnh không cho phép bạn 
thao tác trên dòng dữ liệu 
Con trỏ động cho phép bạn thực 
hiện các thao tác trên dòng dữ 
NHÓM 4_CURSOR 
liệu 
Khi sử dụng con trỏ Keyset 
Driven thì các dòng dữ liệu 
được định hướng cố định 
Chỉ cho phép bạn duyệt từng 
dòng một chiều từ đầu đến 
cuối 
STATIC 
DYNAMIC 
KEYSET DRIVEN 
FORWARD-ONLY 
PHÂN LOẠI
CÚ PHÁP KHAI BÁO 
 CÚ PHÁP CHUẨN: 
DECLARE cur_name CURSOR 
FOR select_statement 
[ FOR {Read only| Update[of column_name [,…n] ] } ] 
Ví dụ: 
DECLARE vt_cur CURSOR 
FOR 
SELECT * FROM ITEMS 
NHÓM 4_CURSOR
 CÚ PHÁP MỞ RỘNG: 
DECLARE cursor_name CURSOR 
[ Local | Global ] 
[ Forward_only| Scroll] 
[ Static| Dynamic] 
[ Read_only] For select_statement 
[ For Update [ of column_name [,…n] ] ] 
NHÓM 4_CURSOR
PHẠM VI 
 Local :chỉ sử dụng trong phạm vi khai 
báo(mặc định) 
 Global :sử dụng chung cho cả kết nối 
NHÓM 4_CURSOR
DI CHUYỂN 
 ForWard_Only :chỉ di chuyển một 
hướng từ trước ra sau(mặc định) 
 Scroll : di chuyển tùy ý 
NHÓM 4_CURSOR
TRẠNG THÁI 
 Static : dữ liệu trên Cursor không thay đổi 
mặc dù dữ liệu trong bảng nguồn thay đổi 
(mặc định) 
 Dynamic :dữ liệu trên Cursor sẽ thay đổi khi 
dữ liệu trong bảng nguồn thay đổi 
 KeySet :giống Dynamic nhưng chỉ thay đổi 
những dòng bị cập nhật 
NHÓM 4_CURSOR
 Bước 1: KHAI BÁO CURSOR 
 Bước 2: MỞ CURSOR 
OPEN <cursor_name> 
 Bước 3: DUYỆT CURSOR 
FETCH < cursor_name > 
 Bước4: ĐÓNG CON TRỎ 
CLOSE < cursor_name > 
 Bước 5: GIẢI PHÓNG CON TRỎ 
DEALLOCATE <Cursor_name> 
NHÓM 4_CURSOR 
CÁCH SỬ DỤNG
CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH) 
• CÚ PHÁP: 
FETCH <hướng di chuyển> From Tên_biến_Cursor Into 
<danh sách biến> 
• Danh sách biến là biến chứa giá trị của cursor 
• VÍ DỤ: 
FETCH NEXT From Cur_MatHang 
INTO @maMH,@tenMH 
NHÓM 4_CURSOR
CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH) (TT) 
• Truy xuất và duyệt con trỏ: 
FETCH FIRST: Truy xuất hàng đầu tiên. 
FETCH NEXT: Truy xuất hàng tiếp theo 
hàng truy xuất trước đó. 
FETCH PRIOR: Truy xuất hàng trước hàng 
truy xuất trước đó. 
FETCH LAST: Truy xuất hàng cuối 
cùng.Truy xuất và duyệt con trỏ 
NHÓM 4_CURSOR
CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH) 
FETCH ABSOLUTE n: 
_ Di chuyển đến mẩu tin thứ n tính từ mẩu tin đầu tiên 
Nếu n là một số nguyên dương, nó sẽ truy xuất hàng trong con 
trỏ. 
_ Nếu n là một số nguyên âm, n hàng trước hàng cuối cùng 
trong con trỏ được truy xuất. 
_ Nếu n bằng 0, không hàng nào được truy xuất. 
_ Ví dụ: FETCH ABSOLUTE 30 from test_cursor -- lấy ra hàng thứ 30 
from CURSOR 
FETCH RELATIVE n: 
_ Di chuyển đến mẩu tin thứ n tính từ mẩu tin hiện hành 
_ Nếu n là số âm, n hàng trước hàng truy xuất trước đóđược 
truy xuất. 
_ Nếu n bằng 0, hàng hiện tại được nhận về. 
NHÓM 4_CURSOR
CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH) 
 @@fetch_status : biến hệ thống cho biết lệnh 
FETCH vừa thực hiện có thành công hay thất 
bại 
nếu giá trị trả về = 0 : lệnh thực hiện thành 
công 
nếu giá trị trả về <> 0: 
lệnh thực hiện thất bại 
NHÓM 4_CURSOR
 TÀI LIỆU THAM KHẢO: 
• Sinhvienit.net 
• www.adth.com 
• Professinal Microsoft SQL Sever 2008 
• Bis.net.vn 
CÁM ƠN CÔ VÀ CÁC BẠN ĐÃ CHÚ Ý 
LẮNG NGHE !!! 
NHÓM 4_CURSOR

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Chủ đề 5_Cursor_Nhóm 4

  • 1. Trường Đại Học Khoa Học Tự Nhiên TPHCM Môn: Quản Trị Cơ Sở Dữ Liệu NHÓM 4 : Lê Thị Kim Trinh _1263146 Hồ Tuấn Vũ _1263159 Nguyễn Văn Tú _1263149 Trương Đặng Huy Hoài _1263081 NHÓM 4_CURSOR
  • 2. Chủ đề 5: CURSOR GIỚI THIỆU NHÓM 4_CURSOR CÁCH SỬ DỤNG
  • 3. GIỚI THIỆU ĐỊNH NGHĨA PHÂN LOẠI CÚ PHÁP KHAI BÁO NHÓM 4_CURSOR
  • 4. Select * From SINHVIEN Where GIOITINH = n’Nam’ ĐỊNH NGHĨA  VẤN ĐỀ: MSSV HOVATEN NGAYSINH GIOITINH 001 Nguyễn Lam 21/01/1994 Nam 002 Đỗ Đình Đan Đan 11/11/1994 Nữ 003 Trần Trung 02/09/1995 Nam 004 Lê Huỳnh Ngọc Minh NHÓM 4_CURSOR 15/12/1994 Nữ MSSV HOVATEN NGAYSINH GIOITINH 001 Nguyễn Lam 21/01/1994 Nam 003 Trần Trung 02/09/1995 Nam
  • 5.  ĐỊNH NGHĨA: Cursor (con trỏ) là một đối tượng cơ sở dữ liệu được sử dụng để khai báo với từng dòng cơ sở dữ liệu. NHÓM 4_CURSOR
  • 6. Con trỏ tĩnh không cho phép bạn thao tác trên dòng dữ liệu Con trỏ động cho phép bạn thực hiện các thao tác trên dòng dữ NHÓM 4_CURSOR liệu Khi sử dụng con trỏ Keyset Driven thì các dòng dữ liệu được định hướng cố định Chỉ cho phép bạn duyệt từng dòng một chiều từ đầu đến cuối STATIC DYNAMIC KEYSET DRIVEN FORWARD-ONLY PHÂN LOẠI
  • 7. CÚ PHÁP KHAI BÁO  CÚ PHÁP CHUẨN: DECLARE cur_name CURSOR FOR select_statement [ FOR {Read only| Update[of column_name [,…n] ] } ] Ví dụ: DECLARE vt_cur CURSOR FOR SELECT * FROM ITEMS NHÓM 4_CURSOR
  • 8.  CÚ PHÁP MỞ RỘNG: DECLARE cursor_name CURSOR [ Local | Global ] [ Forward_only| Scroll] [ Static| Dynamic] [ Read_only] For select_statement [ For Update [ of column_name [,…n] ] ] NHÓM 4_CURSOR
  • 9. PHẠM VI  Local :chỉ sử dụng trong phạm vi khai báo(mặc định)  Global :sử dụng chung cho cả kết nối NHÓM 4_CURSOR
  • 10. DI CHUYỂN  ForWard_Only :chỉ di chuyển một hướng từ trước ra sau(mặc định)  Scroll : di chuyển tùy ý NHÓM 4_CURSOR
  • 11. TRẠNG THÁI  Static : dữ liệu trên Cursor không thay đổi mặc dù dữ liệu trong bảng nguồn thay đổi (mặc định)  Dynamic :dữ liệu trên Cursor sẽ thay đổi khi dữ liệu trong bảng nguồn thay đổi  KeySet :giống Dynamic nhưng chỉ thay đổi những dòng bị cập nhật NHÓM 4_CURSOR
  • 12.  Bước 1: KHAI BÁO CURSOR  Bước 2: MỞ CURSOR OPEN <cursor_name>  Bước 3: DUYỆT CURSOR FETCH < cursor_name >  Bước4: ĐÓNG CON TRỎ CLOSE < cursor_name >  Bước 5: GIẢI PHÓNG CON TRỎ DEALLOCATE <Cursor_name> NHÓM 4_CURSOR CÁCH SỬ DỤNG
  • 13. CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH) • CÚ PHÁP: FETCH <hướng di chuyển> From Tên_biến_Cursor Into <danh sách biến> • Danh sách biến là biến chứa giá trị của cursor • VÍ DỤ: FETCH NEXT From Cur_MatHang INTO @maMH,@tenMH NHÓM 4_CURSOR
  • 14. CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH) (TT) • Truy xuất và duyệt con trỏ: FETCH FIRST: Truy xuất hàng đầu tiên. FETCH NEXT: Truy xuất hàng tiếp theo hàng truy xuất trước đó. FETCH PRIOR: Truy xuất hàng trước hàng truy xuất trước đó. FETCH LAST: Truy xuất hàng cuối cùng.Truy xuất và duyệt con trỏ NHÓM 4_CURSOR
  • 15. CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH) FETCH ABSOLUTE n: _ Di chuyển đến mẩu tin thứ n tính từ mẩu tin đầu tiên Nếu n là một số nguyên dương, nó sẽ truy xuất hàng trong con trỏ. _ Nếu n là một số nguyên âm, n hàng trước hàng cuối cùng trong con trỏ được truy xuất. _ Nếu n bằng 0, không hàng nào được truy xuất. _ Ví dụ: FETCH ABSOLUTE 30 from test_cursor -- lấy ra hàng thứ 30 from CURSOR FETCH RELATIVE n: _ Di chuyển đến mẩu tin thứ n tính từ mẩu tin hiện hành _ Nếu n là số âm, n hàng trước hàng truy xuất trước đóđược truy xuất. _ Nếu n bằng 0, hàng hiện tại được nhận về. NHÓM 4_CURSOR
  • 16. CÁCH SỬ DỤNG_DUYỆT CON TRỎ (FETCH)  @@fetch_status : biến hệ thống cho biết lệnh FETCH vừa thực hiện có thành công hay thất bại nếu giá trị trả về = 0 : lệnh thực hiện thành công nếu giá trị trả về <> 0: lệnh thực hiện thất bại NHÓM 4_CURSOR
  • 17.  TÀI LIỆU THAM KHẢO: • Sinhvienit.net • www.adth.com • Professinal Microsoft SQL Sever 2008 • Bis.net.vn CÁM ƠN CÔ VÀ CÁC BẠN ĐÃ CHÚ Ý LẮNG NGHE !!! NHÓM 4_CURSOR