{"id":81401,"date":"2024-11-25T09:08:46","date_gmt":"2024-11-25T02:08:46","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=81401"},"modified":"2025-10-11T23:55:44","modified_gmt":"2025-10-11T16:55:44","slug":"thu-tuc-trong-sql","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/","title":{"rendered":"Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Thu_tuc_trong_SQL_la_gi\" >Th\u1ee7 t\u1ee5c trong SQL l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Loi_ich_cua_viec_su_dung_thu_tuc_trong_SQL\" >L\u1ee3i \u00edch c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Nhuoc_diem_khi_su_dung_thu_tuc_trong_SQL\" >Nh\u01b0\u1ee3c \u0111i\u1ec3m khi s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Phan_loai_thu_tuc_trong_SQL_theo_nguon_goc\" >Ph\u00e2n lo\u1ea1i th\u1ee7 t\u1ee5c trong SQL theo ngu\u1ed3n g\u1ed1c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Phan_loai_thu_tuc_trong_SQL_theo_kien_truc\" >Ph\u00e2n lo\u1ea1i th\u1ee7 t\u1ee5c trong SQL theo ki\u1ebfn tr\u00fac<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Phan_loai_thu_tuc_trong_SQL_theo_chuc_nang\" >Ph\u00e2n lo\u1ea1i th\u1ee7 t\u1ee5c trong SQL theo ch\u1ee9c n\u0103ng<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Cu_phap_thu_tuc_trong_SQL_duoc_dinh_nghia_san\" >C\u00fa ph\u00e1p th\u1ee7 t\u1ee5c trong SQL \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a s\u1eb5n<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Cu_phap_thu_tuc_trong_SQL_do_nguoi_dung_dinh_nghia\" >C\u00fa ph\u00e1p th\u1ee7 t\u1ee5c trong SQL do ng\u01b0\u1eddi d\u00f9ng \u0111\u1ecbnh ngh\u0129a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Cac_cau_hoi_thuong_gap_ve_thu_tuc_trong_SQL\" >C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 th\u1ee7 t\u1ee5c trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#Tong_ket_thu_tuc_trong_SQL\" >T\u1ed5ng k\u1ebft th\u1ee7 t\u1ee5c trong SQL<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><em><strong>Th\u1ee7 t\u1ee5c (Stored Procedure) l\u00e0 m\u1ed9t ph\u1ea7n quan tr\u1ecdng trong SQL, gi\u00fap th\u1ef1c hi\u1ec7n t\u1ef1 \u0111\u1ed9ng c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p v\u00e0 l\u1eb7p l\u1ea1i. Vi\u1ec7c hi\u1ec3u r\u00f5 v\u00e0 n\u1eafm v\u1eefng c\u00e1ch s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL s\u1ebd gi\u00fap t\u0103ng c\u01b0\u1eddng b\u1ea3o m\u1eadt, t\u0103ng hi\u1ec7u su\u1ea5t v\u00e0 gi\u1ea3m thi\u1ec3u r\u1ee7i ro cho c\u01a1 s\u1edf d\u1eef li\u1ec7u v\u00e0 \u1ee9ng d\u1ee5ng.<\/strong><\/em><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Th\u1ee7 t\u1ee5c l\u00e0 g\u00ec?<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">L\u1ee3i \u00edch v\u00e0 l\u01b0u \u00fd khi s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Ph\u00e2n lo\u1ea1i th\u1ee7 t\u1ee5c<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 c\u00fa ph\u00e1p v\u1edbi th\u1ee7 t\u1ee5c<\/span><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Xem th\u00eam: <a href=\"https:\/\/itviec.com\/viec-lam-it\/sql?utm_medium=anchor_text_high&amp;utm_source=blog&amp;utm_campaign=viec_lam_sql&amp;utm_content=thu_tuc_trong_sql\" target=\"_blank\" rel=\"noreferrer noopener\">Vi\u1ec7c l\u00e0m SQL<\/a> n\u1ed5i b\u1eadt tr\u00ean ITviec<\/strong><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-th\u1ee7-t\u1ee5c-trong-sql-la-gi\"><span class=\"ez-toc-section\" id=\"Thu_tuc_trong_SQL_la_gi\"><\/span><b>Th\u1ee7 t\u1ee5c trong SQL l\u00e0 g\u00ec?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong SQL, th\u1ee7 t\u1ee5c (Procedure), hay c\u00f2n \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 th\u1ee7 t\u1ee5c l\u01b0u tr\u1eef (Stored Procedure) l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh SQL \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, ho\u1ea1t \u0111\u1ed9ng nh\u01b0 m\u1ed9t ti\u1ec3u ch\u01b0\u01a1ng tr\u00ecnh (subroutine) v\u00e0 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi ra \u0111\u1ec3 th\u1ef1c hi\u1ec7n trong c\u00e1c h\u00e0nh \u0111\u1ed9ng c\u1ee5 th\u1ec3. Th\u1ee7 t\u1ee5c gi\u00fap th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 nh\u01b0 truy v\u1ea5n, bi\u1ebfn \u0111\u1ed5i, c\u1eadp nh\u1eadt d\u1eef li\u1ec7u, t\u1ea1o b\u00e1o c\u00e1o c\u01a1 b\u1ea3n, t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng v\u00e0 t\u0103ng c\u01b0\u1eddng b\u1ea3o m\u1eadt c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Th\u1ee7 t\u1ee5c c\u00f2n h\u1ed7 tr\u1ee3 vi\u1ec7c truy\u1ec1n tham s\u1ed1, cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng cung c\u1ea5p d\u1eef li\u1ec7u \u0111\u1ea7u v\u00e0o \u0111\u1ec3 th\u1ee7 t\u1ee5c th\u1ef1c thi c\u00e1c t\u00e1c v\u1ee5 c\u1ee5 th\u1ec3 d\u1ef1a tr\u00ean c\u00e1c gi\u00e1 tr\u1ecb n\u00e0y. Nh\u1edd \u0111\u00f3, th\u1ee7 t\u1ee5c t\u0103ng c\u01b0\u1eddng t\u00ednh hi\u1ec7u qu\u1ea3, t\u00ednh t\u00e1i s\u1eed d\u1ee5ng v\u00e0 b\u1ea3o m\u1eadt trong qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u, \u0111\u1ed3ng th\u1eddi gi\u1ea3m thi\u1ec3u vi\u1ec7c ph\u1ea3i vi\u1ebft l\u1ea1i c\u00e1c <a href=\"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/\" target=\"_blank\" rel=\"noopener\"><strong>c\u00e2u l\u1ec7nh SQL<\/strong><\/a> nhi\u1ec1u l\u1ea7n.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-l\u1ee3i-ich-c\u1ee7a-vi\u1ec7c-s\u1eed-d\u1ee5ng-th\u1ee7-t\u1ee5c-trong-sql\"><span class=\"ez-toc-section\" id=\"Loi_ich_cua_viec_su_dung_thu_tuc_trong_SQL\"><\/span><b>L\u1ee3i \u00edch c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>C\u00f3 th\u1ec3 t\u00e1i s\u1eed d\u1ee5ng:<\/b><span style=\"font-weight: 400;\"> Th\u1ee7 t\u1ee5c c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng l\u1ea1i b\u1edfi nhi\u1ec1u ng\u01b0\u1eddi d\u00f9ng v\u00e0 \u1ee9ng d\u1ee5ng ch\u1ec9 b\u1eb1ng c\u00e1ch g\u1ecdi n\u00f3, gi\u00fap gi\u1ea3m thi\u1ec3u vi\u1ec7c ph\u1ea3i vi\u1ebft l\u1ea1i c\u00e1c c\u00e2u l\u1ec7nh SQL nhi\u1ec1u l\u1ea7n v\u00e0 ti\u1ebft ki\u1ec7m th\u1eddi gian.<\/span><\/li>\n\n\n\n<li><b>D\u1ec5 ch\u1ec9nh s\u1eeda:<\/b><span style=\"font-weight: 400;\"> B\u1ea1n c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng thay \u0111\u1ed5i n\u1ed9i dung b\u00ean trong th\u1ee7 t\u1ee5c b\u1eb1ng l\u1ec7nh ALTER, gi\u00fap \u0111i\u1ec1u ch\u1ec9nh nhanh ch\u00f3ng khi c\u00f3 y\u00eau c\u1ea7u m\u1edbi.<\/span><\/li>\n\n\n\n<li><b>T\u0103ng c\u01b0\u1eddng b\u1ea3o m\u1eadt:<\/b><span style=\"font-weight: 400;\"> Th\u1ee7 t\u1ee5c gi\u00fap h\u1ea1n ch\u1ebf truy c\u1eadp tr\u1ef1c ti\u1ebfp v\u00e0o b\u1ea3ng, t\u1eeb \u0111\u00f3 n\u00e2ng cao m\u1ee9c \u0111\u1ed9 b\u1ea3o m\u1eadt cho c\u01a1 s\u1edf d\u1eef li\u1ec7u ho\u1eb7c \u1ee9ng d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><b>Gi\u1ea3m l\u01b0u l\u01b0\u1ee3ng m\u1ea1ng:<\/b><span style=\"font-weight: 400;\"> Thay v\u00ec truy\u1ec1n to\u00e0n b\u1ed9 c\u00e2u truy v\u1ea5n, m\u00e1y ch\u1ee7 ch\u1ec9 c\u1ea7n g\u1ecdi t\u00ean th\u1ee7 t\u1ee5c, gi\u00fap gi\u1ea3m t\u1ea3i l\u01b0u l\u01b0\u1ee3ng m\u1ea1ng.<\/span><\/li>\n\n\n\n<li><b>T\u0103ng hi\u1ec7u su\u1ea5t:<\/b><span style=\"font-weight: 400;\"> Khi \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng l\u1ea7n \u0111\u1ea7u, th\u1ee7 t\u1ee5c s\u1ebd \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a v\u00e0 l\u01b0u v\u00e0o b\u1ed9 \u0111\u1ec7m, gi\u00fap th\u1ef1c thi nhanh ch\u00f3ng cho nh\u1eefng l\u1ea7n ti\u1ebfp theo.<\/span><\/li>\n\n\n\n<li><b>D\u1ec5 b\u1ea3o tr\u00ec:<\/b><span style=\"font-weight: 400;\"> Khi c\u00f3 nhi\u1ec1u n\u01a1i c\u00f9ng s\u1eed d\u1ee5ng m\u1ed9t logic, th\u1ee7 t\u1ee5c gi\u00fap vi\u1ec7c b\u1ea3o tr\u00ec \u0111\u01a1n gi\u1ea3n h\u01a1n. N\u1ebfu logic c\u1ea7n thay \u0111\u1ed5i, ch\u1ec9 c\u1ea7n ch\u1ec9nh s\u1eeda th\u1ee7 t\u1ee5c thay v\u00ec ph\u1ea3i c\u1eadp nh\u1eadt \u1edf nhi\u1ec1u n\u01a1i kh\u00e1c nhau.<\/span><\/li>\n\n\n\n<li><b>Gi\u1ea3m thi\u1ec3u r\u1ee7i ro l\u1ed7i:<\/b><span style=\"font-weight: 400;\"> Logic \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf s\u1eb5n trong th\u1ee7 t\u1ee5c s\u1ebd gi\u1ea3m b\u1edbt r\u1ee7i ro l\u1ed7i do ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c h\u1ec7 th\u1ed1ng g\u00e2y ra, b\u1edfi v\u00ec c\u00e1c n\u1ed9i dung quan tr\u1ecdng \u0111\u00e3 \u0111\u01b0\u1ee3c c\u1ed1 \u0111\u1ecbnh tr\u01b0\u1edbc.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-nh\u01b0\u1ee3c-di\u1ec3m-khi-s\u1eed-d\u1ee5ng-th\u1ee7-t\u1ee5c-trong-sql\"><span class=\"ez-toc-section\" id=\"Nhuoc_diem_khi_su_dung_thu_tuc_trong_SQL\"><\/span><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m khi s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Ph\u1ee5 thu\u1ed9c v\u00e0o h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u:<\/b><span style=\"font-weight: 400;\"> M\u1ed7i h\u1ec7 qu\u1ea3n tr\u1ecb CSDL s\u1eed d\u1ee5ng ng\u00f4n ng\u1eef ri\u00eang n\u00ean khi chuy\u1ec3n sang h\u1ec7 qu\u1ea3n tr\u1ecb kh\u00e1c, b\u1ea1n c\u00f3 th\u1ec3 c\u1ea7n vi\u1ebft l\u1ea1i cho ph\u00f9 h\u1ee3p.<\/span><\/li>\n\n\n\n<li><b>T\u00ednh n\u0103ng kh\u00e1c nhau:<\/b><span style=\"font-weight: 400;\"> Ng\u00f4n ng\u1eef th\u1ee7 t\u1ee5c c\u1ee7a m\u1ed7i nh\u00e0 cung c\u1ea5p c\u00f3 c\u00e1c t\u00ednh n\u0103ng v\u00e0 kh\u1ea3 n\u0103ng h\u1ed7 tr\u1ee3 kh\u00e1c nhau. V\u00ed d\u1ee5, PL\/SQL c\u1ee7a Oracle c\u00f3 nhi\u1ec1u ch\u1ee9c n\u0103ng n\u00e2ng cao h\u01a1n so v\u1edbi T-SQL c\u1ee7a Microsoft.<\/span><\/li>\n\n\n\n<li><b>H\u1ed7 tr\u1ee3 c\u00f4ng c\u1ee5 c\u00f2n h\u1ea1n ch\u1ebf:<\/b><span style=\"font-weight: 400;\"> C\u00e1c c\u00f4ng c\u1ee5 \u0111\u1ec3 vi\u1ebft v\u00e0 g\u1ee1 l\u1ed7i th\u1ee7 t\u1ee5c th\u01b0\u1eddng kh\u00f4ng \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 t\u1ed1t nh\u01b0 c\u00e1c ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh kh\u00e1c. M\u1ee9c \u0111\u1ed9 h\u1ed7 tr\u1ee3 n\u00e0y c\u00f2n t\u00f9y thu\u1ed9c v\u00e0o nh\u00e0 cung c\u1ea5p v\u00e0 ng\u00f4n ng\u1eef b\u1ea1n s\u1eed d\u1ee5ng.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-phan-lo\u1ea1i-th\u1ee7-t\u1ee5c-trong-sql-theo-ngu\u1ed3n-g\u1ed1c\"><span class=\"ez-toc-section\" id=\"Phan_loai_thu_tuc_trong_SQL_theo_nguon_goc\"><\/span><b>Ph\u00e2n lo\u1ea1i th\u1ee7 t\u1ee5c trong SQL theo ngu\u1ed3n g\u1ed1c<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ee7-t\u1ee5c-co-s\u1eb5n\"><b>Th\u1ee7 t\u1ee5c c\u00f3 s\u1eb5n<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">SQL cung c\u1ea5p m\u1ed9t s\u1ed1 th\u1ee7 t\u1ee5c \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a s\u1eb5n nh\u1eb1m th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 qu\u1ea3n l\u00fd v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u c\u01a1 b\u1ea3n. C\u00e1c th\u1ee7 t\u1ee5c n\u00e0y c\u00f3 s\u1eb5n trong h\u1ec7 th\u1ed1ng c\u1ee7a m\u1ed9t s\u1ed1 DBMS nh\u01b0 SQL Server v\u00e0 gi\u00fap ng\u01b0\u1eddi d\u00f9ng th\u1ef1c hi\u1ec7n nhanh ch\u00f3ng c\u00e1c thao t\u00e1c th\u01b0\u1eddng g\u1eb7p nh\u01b0 l\u1ea5y th\u00f4ng tin v\u1ec1 b\u1ea3ng, c\u1ed9t, ho\u1eb7c k\u1ebft n\u1ed1i. V\u00ed d\u1ee5 v\u1ec1 c\u00e1c th\u1ee7 t\u1ee5c \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a s\u1eb5n ph\u1ed5 bi\u1ebfn c\u00f3 th\u1ec3 bao g\u1ed3m:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>T\u00ean th\u1ee7 t\u1ee5c<\/b><\/td><td><b>M\u00f4 t\u1ea3<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">sp_rename<\/span><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 \u0111\u1ed5i t\u00ean m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 th\u1ee7 t\u1ee5c, b\u1ea3ng, view,&#8230;<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">sp_changeowner<\/span><\/td><td><span style=\"font-weight: 400;\">Thay \u0111\u1ed5i quy\u1ec1n s\u1edf h\u1eefu c\u1ee7a m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">sp_help<\/span><\/td><td><span style=\"font-weight: 400;\">Cung c\u1ea5p chi ti\u1ebft v\u1ec1 b\u1ea5t k\u1ef3 \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0o trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">sp_helpdb<\/span><\/td><td><span style=\"font-weight: 400;\">Hi\u1ec3n th\u1ecb chi ti\u1ebft v\u1ec1 c\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a trong SQL Server.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">sp_helptext<\/span><\/td><td><span style=\"font-weight: 400;\">Cung c\u1ea5p m\u00e3 ngu\u1ed3n c\u1ee7a m\u1ed9t th\u1ee7 t\u1ee5c \u0111\u00e3 l\u01b0u trong SQL Server.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">sp_depends<\/span><\/td><td><span style=\"font-weight: 400;\">Hi\u1ec3n th\u1ecb th\u00f4ng tin v\u1ec1 t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng ph\u1ee5 thu\u1ed9c v\u00e0o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee5 th\u1ec3 trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ee7-t\u1ee5c-do-ng\u01b0\u1eddi-dung-t\u1ea1o-user-defined-stored-procedures\"><b>Th\u1ee7 t\u1ee5c do ng\u01b0\u1eddi d\u00f9ng t\u1ea1o (User-defined Stored Procedures)<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Lo\u1ea1i th\u1ee7 t\u1ee5c n\u00e0y \u0111\u01b0\u1ee3c t\u1ea1o b\u1edfi ng\u01b0\u1eddi d\u00f9ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 c\u1ee5 th\u1ec3 cho y\u00eau c\u1ea7u ri\u00eang bi\u1ec7t. Ng\u01b0\u1eddi d\u00f9ng c\u00f3 th\u1ec3 t\u1ef1 thi\u1ebft k\u1ebf logic ph\u00f9 h\u1ee3p v\u1edbi m\u1ee5c ti\u00eau c\u1ee7a h\u1ecd.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-phan-lo\u1ea1i-th\u1ee7-t\u1ee5c-trong-sql-theo-ki\u1ebfn-truc\"><span class=\"ez-toc-section\" id=\"Phan_loai_thu_tuc_trong_SQL_theo_kien_truc\"><\/span><b>Ph\u00e2n lo\u1ea1i th\u1ee7 t\u1ee5c trong SQL theo ki\u1ebfn tr\u00fac<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Native SQL procedure<\/b><span style=\"font-weight: 400;\">: L\u00e0 th\u1ee7 t\u1ee5c \u0111\u01b0\u1ee3c vi\u1ebft ho\u00e0n to\u00e0n b\u1eb1ng SQL, bao g\u1ed3m ng\u00f4n ng\u1eef th\u1ee7 t\u1ee5c SQL (SQL PL). Th\u1ee7 t\u1ee5c n\u00e0y \u0111\u01b0\u1ee3c t\u1ea1o ra b\u1eb1ng m\u1ed9t c\u00e2u l\u1ec7nh duy nh\u1ea5t, kh\u00f4ng y\u00eau c\u1ea7u bi\u00ean d\u1ecbch hay li\u00ean k\u1ebft v\u1edbi ch\u01b0\u01a1ng tr\u00ecnh b\u00ean ngo\u00e0i. Native SQL procedure c\u00f3 hi\u1ec7u su\u1ea5t cao v\u00e0 h\u1ed7 tr\u1ee3 nhi\u1ec1u t\u00ednh n\u0103ng ph\u1ee9c t\u1ea1p, nh\u01b0 qu\u1ea3n l\u00fd nhi\u1ec1u phi\u00ean b\u1ea3n ho\u1eb7c th\u1ef1c hi\u1ec7n c\u00e1c l\u1ec7nh k\u1ebft h\u1ee3p l\u1ed3ng nhau.<\/span><\/li>\n\n\n\n<li><b>Th\u1ee7 t\u1ee5c ngo\u1ea1i vi (External SQL procedure): <\/b><span style=\"font-weight: 400;\">Lo\u1ea1i th\u1ee7 t\u1ee5c n\u00e0y c\u0169ng \u0111\u01b0\u1ee3c vi\u1ebft b\u1eb1ng SQL, nh\u01b0ng c\u1ea7n ph\u1ea3i li\u00ean k\u1ebft v\u1edbi ch\u01b0\u01a1ng tr\u00ecnh b\u00ean ngo\u00e0i (th\u01b0\u1eddng l\u00e0 C). Th\u1ee7 t\u1ee5c ngo\u1ea1i vi \u0111\u00e3 b\u1ecb h\u1ea1n ch\u1ebf trong c\u00e1c phi\u00ean b\u1ea3n SQL m\u1edbi do hi\u1ec7u su\u1ea5t k\u00e9m h\u01a1n v\u00e0 ph\u1ee9c t\u1ea1p h\u01a1n trong vi\u1ec7c tri\u1ec3n khai.<\/span><\/li>\n\n\n\n<li><b>Th\u1ee7 t\u1ee5c l\u1ed3ng nhau (Nested Stored Procedures):<\/b><span style=\"font-weight: 400;\"> L\u00e0 th\u1ee7 t\u1ee5c c\u00f3 th\u1ec3 g\u1ecdi m\u1ed9t ho\u1eb7c nhi\u1ec1u th\u1ee7 t\u1ee5c kh\u00e1c b\u00ean trong n\u00f3, gi\u00fap t\u0103ng t\u00ednh modular cho c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p, cho ph\u00e9p ph\u00e2n chia v\u00e0 t\u1ed5 ch\u1ee9c c\u00e1c quy tr\u00ecnh m\u1ed9t c\u00e1ch logic.<\/span><\/li>\n\n\n\n<li><b>Th\u1ee7 t\u1ee5c \u0111\u1ec7 quy (Recursive Stored Procedures):<\/b><span style=\"font-weight: 400;\"> \u0110\u00e2y l\u00e0 lo\u1ea1i th\u1ee7 t\u1ee5c c\u00f3 kh\u1ea3 n\u0103ng g\u1ecdi ch\u00ednh n\u00f3 trong qu\u00e1 tr\u00ecnh th\u1ef1c thi. Th\u1ee7 t\u1ee5c \u0111\u1ec7 quy th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 y\u00eau c\u1ea7u l\u1eb7p l\u1ea1i m\u1ed9t c\u00e1ch tu\u1ea7n t\u1ef1 nh\u01b0 t\u00ednh giai th\u1eeba ho\u1eb7c duy\u1ec7t qua c\u00e2y ph\u00e2n c\u1ea5p.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-phan-lo\u1ea1i-th\u1ee7-t\u1ee5c-trong-sql-theo-ch\u1ee9c-nang\"><span class=\"ez-toc-section\" id=\"Phan_loai_thu_tuc_trong_SQL_theo_chuc_nang\"><\/span><b>Ph\u00e2n lo\u1ea1i th\u1ee7 t\u1ee5c trong SQL theo ch\u1ee9c n\u0103ng<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Th\u1ee7 t\u1ee5c c\u00f3 tham s\u1ed1:<\/b><span style=\"font-weight: 400;\"> L\u00e0 th\u1ee7 t\u1ee5c nh\u1eadn m\u1ed9t ho\u1eb7c nhi\u1ec1u tham s\u1ed1 \u0111\u1ea7u v\u00e0o t\u1eeb ng\u01b0\u1eddi d\u00f9ng, cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 d\u1ef1a tr\u00ean c\u00e1c gi\u00e1 tr\u1ecb truy\u1ec1n v\u00e0o, t\u1eeb \u0111\u00f3 t\u0103ng t\u00ednh linh ho\u1ea1t trong x\u1eed l\u00fd d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>Th\u1ee7 t\u1ee5c kh\u00f4ng c\u00f3 tham s\u1ed1: <\/b><span style=\"font-weight: 400;\">Th\u1ee7 t\u1ee5c n\u00e0y th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 c\u1ed1 \u0111\u1ecbnh, kh\u00f4ng thay \u0111\u1ed5i v\u00e0 kh\u00f4ng nh\u1eadn tham s\u1ed1 \u0111\u1ea7u v\u00e0o. \u0110\u00e2y l\u00e0 lo\u1ea1i th\u1ee7 t\u1ee5c \u0111\u01a1n gi\u1ea3n, ch\u1ee7 y\u1ebfu \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c c\u1ee5 th\u1ec3 trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>Th\u1ee7 t\u1ee5c t\u1ea1m th\u1eddi: <\/b><span style=\"font-weight: 400;\">L\u00e0 th\u1ee7 t\u1ee5c ch\u1ec9 t\u1ed3n t\u1ea1i trong m\u1ed9t phi\u00ean l\u00e0m vi\u1ec7c v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 ng\u1eafn h\u1ea1n ho\u1eb7c th\u1eed nghi\u1ec7m. Th\u1ee7 t\u1ee5c n\u00e0y gi\u00fap tr\u00e1nh t\u00e1c \u0111\u1ed9ng l\u00e2u d\u00e0i l\u00ean c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n\n\n\n<p>Ti\u1ebfp theo, ta s\u1ebd c\u00f9ng \u0111i qua c\u00e1c c\u00fa ph\u00e1p v\u1edbi th\u1ee7 t\u1ee5c trong SQL b\u1eb1ng v\u00ed d\u1ee5 th\u1ef1c t\u1ebf. <span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed ta c\u00f3 b\u1ea3ng Employees sau:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>ID<\/b><\/td><td><b>Name<\/b><\/td><td><b>Department_ID<\/b><\/td><td><b>Position<\/b><\/td><td><b>Salary<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">John Doe<\/span><\/td><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">Manager<\/span><\/td><td><span style=\"font-weight: 400;\">8000<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">2<\/span><\/td><td><span style=\"font-weight: 400;\">Jane Smith<\/span><\/td><td><span style=\"font-weight: 400;\">2<\/span><\/td><td><span style=\"font-weight: 400;\">Developer<\/span><\/td><td><span style=\"font-weight: 400;\">5500<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">3<\/span><\/td><td><span style=\"font-weight: 400;\">Tom Lee<\/span><\/td><td><span style=\"font-weight: 400;\">2<\/span><\/td><td><span style=\"font-weight: 400;\">Analyst<\/span><\/td><td><span style=\"font-weight: 400;\">6000<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">4<\/span><\/td><td><span style=\"font-weight: 400;\">Anna Brown<\/span><\/td><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">Clerk<\/span><\/td><td><span style=\"font-weight: 400;\">3000<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u01b0u \u00fd: C\u00fa ph\u00e1p v\u1edbi th\u1ee7 t\u1ee5c c\u00f3 th\u1ec3 kh\u00e1c nhau gi\u1eefa c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u (DBMS) nh\u01b0 SQL Server, MySQL, Oracle,&#8230; D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c c\u00fa ph\u00e1p ph\u1ed5 bi\u1ebfn d\u00e0nh ri\u00eang cho SQL Server.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u1ee8ng tuy\u1ec3n <a href=\"https:\/\/itviec.com\/viec-lam-it\/sql?utm_medium=anchor_text_mid&amp;utm_source=blog&amp;utm_campaign=viec_lam_sql&amp;utm_content=thu_tuc_trong_sql\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m SQL<\/a> n\u1ed5i b\u1eadt ngay tr\u00ean ITviec<\/strong><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cu-phap-th\u1ee7-t\u1ee5c-trong-sql-d\u01b0\u1ee3c-d\u1ecbnh-nghia-s\u1eb5n\"><span class=\"ez-toc-section\" id=\"Cu_phap_thu_tuc_trong_SQL_duoc_dinh_nghia_san\"><\/span><b>C\u00fa ph\u00e1p th\u1ee7 t\u1ee5c trong SQL \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a s\u1eb5n<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin chi ti\u1ebft v\u1ec1 b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Employees<\/span><\/i><span style=\"font-weight: 400;\"> trong SQL Server, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c <\/span><span style=\"font-weight: 400;\">sp_help<\/span><span style=\"font-weight: 400;\"> nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>EXEC sp_help Employees;<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 \u0111\u1ed5i t\u00ean c\u1ed9t t\u1eeb <\/span><i><span style=\"font-weight: 400;\">EmployeeName<\/span><\/i><span style=\"font-weight: 400;\"> th\u00e0nh <\/span><i><span style=\"font-weight: 400;\">FullName<\/span><\/i><span style=\"font-weight: 400;\"> trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Employees<\/span><\/i><span style=\"font-weight: 400;\">, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c <\/span><span style=\"font-weight: 400;\">sp_rename<\/span><span style=\"font-weight: 400;\"> nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">EXEC sp_rename 'Employees.EmployeeName', 'FullName', 'COLUMN';<\/span><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cu-phap-th\u1ee7-t\u1ee5c-trong-sql-do-ng\u01b0\u1eddi-dung-d\u1ecbnh-nghia\"><span class=\"ez-toc-section\" id=\"Cu_phap_thu_tuc_trong_SQL_do_nguoi_dung_dinh_nghia\"><\/span><b>C\u00fa ph\u00e1p th\u1ee7 t\u1ee5c trong SQL do ng\u01b0\u1eddi d\u00f9ng \u0111\u1ecbnh ngh\u0129a<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-create-procedure-t\u1ea1o-th\u1ee7-t\u1ee5c\"><b>Create procedure &#8211; T\u1ea1o th\u1ee7 t\u1ee5c<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">CREATE PROCEDURE<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u1ea1o m\u1ed9t th\u1ee7 t\u1ee5c m\u1edbi trong SQL Server. Th\u1ee7 t\u1ee5c n\u00e0y c\u00f3 th\u1ec3 bao g\u1ed3m c\u00e1c c\u00e2u l\u1ec7nh SQL nh\u1eb1m th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 nh\u01b0 truy v\u1ea5n, c\u1eadp nh\u1eadt ho\u1eb7c x\u00f3a d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE PROCEDURE t\u00ean-th\u1ee7-t\u1ee5c&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">-- C\u00e1c c\u00e2u l\u1ec7nh SQL th\u1ef1c thi&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t th\u1ee7 t\u1ee5c l\u1ea5y to\u00e0n b\u1ed9 d\u1eef li\u1ec7u t\u1eeb b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Employees<\/span><\/i><span style=\"font-weight: 400;\"> ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE PROCEDURE GetAllEmployees&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">SELECT * FROM Employees;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-execute-procedure-th\u1ef1c-thi-th\u1ee7-t\u1ee5c\"><b>Execute procedure &#8211; Th\u1ef1c thi th\u1ee7 t\u1ee5c<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1ec7nh <\/span><span style=\"font-weight: 400;\">EXEC<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">EXECUTE<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 g\u1ecdi v\u00e0 th\u1ef1c thi m\u1ed9t th\u1ee7 t\u1ee5c \u0111\u00e3 \u0111\u01b0\u1ee3c t\u1ea1o tr\u01b0\u1edbc \u0111\u00f3 trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">EXEC t\u00ean-th\u1ee7-t\u1ee5c;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 th\u1ef1c thi th\u1ee7 t\u1ee5c <\/span><span style=\"font-weight: 400;\">GetAllEmployees <\/span><span style=\"font-weight: 400;\">\u0111\u00e3 t\u1ea1o \u1edf tr\u00ean th\u00ec ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">EXEC GetAllEmployees;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-alter-procedure-s\u1eeda-d\u1ed5i-th\u1ee7-t\u1ee5c\"><b>Alter procedure &#8211; S\u1eeda \u0111\u1ed5i th\u1ee7 t\u1ee5c<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 s\u1eeda \u0111\u1ed5i m\u1ed9t th\u1ee7 t\u1ee5c \u0111\u00e3 t\u1ed3n t\u1ea1i hay thay \u0111\u1ed5i n\u1ed9i dung ho\u1eb7c c\u1ea5u tr\u00fac c\u1ee7a th\u1ee7 t\u1ee5c, ta s\u1eed d\u1ee5ng l\u1ec7nh&nbsp; <\/span><span style=\"font-weight: 400;\">ALTER PROCEDURE<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">ALTER PROCEDURE t\u00ean-th\u1ee7-t\u1ee5c&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">-- C\u00e1c c\u00e2u l\u1ec7nh SQL m\u1edbi&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 ch\u1ec9nh s\u1eeda l\u1ea1i th\u1ee7 t\u1ee5c <\/span><span style=\"font-weight: 400;\">GetAllEmployees<\/span><span style=\"font-weight: 400;\"> thay v\u00ec tr\u1ea3 v\u1ec1 to\u00e0n b\u1ed9 th\u00f4ng tin c\u1ee7a nh\u00e2n vi\u00ean th\u00e0nh ch\u1ec9 tr\u1ea3 v\u1ec1 t\u00ean v\u00e0 ch\u1ee9c v\u1ee5 c\u1ee7a nh\u00e2n vi\u00ean, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">ALTER PROCEDURE GetAllEmployees&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN SELECT EmployeeName, Position FROM Employees;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-drop-procedure-xoa-th\u1ee7-t\u1ee5c\"><b>Drop procedure &#8211; Xo\u00e1 th\u1ee7 t\u1ee5c<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1ec7nh <\/span><span style=\"font-weight: 400;\">DROP PROCEDURE<\/span><span style=\"font-weight: 400;\"> d\u00f9ng \u0111\u1ec3 x\u00f3a m\u1ed9t th\u1ee7 t\u1ee5c \u0111\u00e3 t\u1ed3n t\u1ea1i kh\u1ecfi c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">DROP PROCEDURE t\u00ean-th\u1ee7-t\u1ee5c;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 xo\u00e1 th\u1ee7 t\u1ee5c <\/span><span style=\"font-weight: 400;\">GetAllEmployees<\/span><span style=\"font-weight: 400;\">, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">DROP PROCEDURE GetAllEmployees;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ee7-t\u1ee5c-v\u1edbi-m\u1ed9t-tham-s\u1ed1\"><b>Th\u1ee7 t\u1ee5c v\u1edbi m\u1ed9t tham s\u1ed1<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u00e0 thh\u1ee7 t\u1ee5c c\u00f3 th\u1ec3 nh\u1eadn m\u1ed9t tham s\u1ed1 \u0111\u1ea7u v\u00e0o \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 c\u1ee5 th\u1ec3 d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb c\u1ee7a tham s\u1ed1.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE PROCEDURE t\u00ean-th\u1ee7-t\u1ee5c (@ThamSo LoaiDuLieu)&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">-- C\u00e1c c\u00e2u l\u1ec7nh SQL s\u1eed d\u1ee5ng tham s\u1ed1&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o th\u1ee7 t\u1ee5c <\/span><span style=\"font-weight: 400;\">GetEmployeeByID<\/span><span style=\"font-weight: 400;\"> nh\u1eadn m\u1ed9t tham s\u1ed1 <\/span><span style=\"font-weight: 400;\">@EmployeeID<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 truy v\u1ea5n th\u00f4ng tin nh\u00e2n vi\u00ean theo ID ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE PROCEDURE GetEmployeeByID @EmployeeID INT&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">SELECT * FROM Employees WHEREEmployeeID = @EmployeeID;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ee7-t\u1ee5c-v\u1edbi-nhi\u1ec1u-tham-s\u1ed1\"><b>Th\u1ee7 t\u1ee5c v\u1edbi nhi\u1ec1u tham s\u1ed1<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u00e0 th\u1ee7 t\u1ee5c c\u00f3 th\u1ec3 nh\u1eadn <\/span><b>nhi\u1ec1u<\/b><span style=\"font-weight: 400;\"> tham s\u1ed1 \u0111\u1ea7u v\u00e0o, cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p d\u1ef1a tr\u00ean nhi\u1ec1u gi\u00e1 tr\u1ecb kh\u00e1c nhau.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE PROCEDURE TenThuTuc (@ThamSo1 LoaiDuLieu, @ThamSo2 LoaiDuLieu)&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">-- C\u00e1c c\u00e2u l\u1ec7nh SQL s\u1eed d\u1ee5ng nhi\u1ec1u tham s\u1ed1&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o th\u1ee7 t\u1ee5c <\/span><span style=\"font-weight: 400;\">GetEmpByDepartmentAndSalary<\/span><span style=\"font-weight: 400;\"> nh\u1eadn hai tham s\u1ed1 <\/span><span style=\"font-weight: 400;\">@DepartmentID<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">@MinSalary<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u00ecm c\u00e1c nh\u00e2n vi\u00ean trong m\u1ed9t ph\u00f2ng ban c\u00f3 m\u1ee9c l\u01b0\u01a1ng t\u1ed1i thi\u1ec3u nh\u1ea5t \u0111\u1ecbnh ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">CREATE PROCEDURE GetEmployeesByDepartmentAndSalary @DepartmentID INT, @MinSalary DECIMAL&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">AS<\/span>\n\n<span style=\"font-weight: 400;\">BEGIN SELECT * FROM Employees WHERE DepartmentID = @DepartmentID AND Salary &gt;= @MinSalary;&nbsp;<\/span>\n\n<span style=\"font-weight: 400;\">END;<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 th\u1ef1c thi th\u1ee7 t\u1ee5c tr\u00ean, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span style=\"font-weight: 400;\">EXEC GetEmployeesByDepartmentAndSalary @DepartmentID = 2, @MinSalary = 5000;<\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-th\u01b0\u1eddng-g\u1eb7p-v\u1ec1-th\u1ee7-t\u1ee5c-trong-sql\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_thuong_gap_ve_thu_tuc_trong_SQL\"><\/span><b>C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 th\u1ee7 t\u1ee5c trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ee7-t\u1ee5c-khac-gi-so-v\u1edbi-ham-function-trong-sql\"><b>Th\u1ee7 t\u1ee5c kh\u00e1c g\u00ec so v\u1edbi h\u00e0m (Function) trong SQL?<\/b><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>Th\u1ee7 t\u1ee5c (Procedure)<\/b><\/td><td><b>H\u00e0m (Function)<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Ch\u1ee9c n\u0103ng<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5, bao g\u1ed3m thao t\u00e1c d\u1eef li\u1ec7u (DML) nh\u01b0 <\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\">, v\u00e0 c\u1ea3 c\u00e1c l\u1ec7nh <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">.<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n t\u00ednh to\u00e1n v\u00e0 tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb, th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng trong c\u00e1c truy v\u1ea5n.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 nhi\u1ec1u gi\u00e1 tr\u1ecb th\u00f4ng qua tham s\u1ed1 OUT ho\u1eb7c kh\u00f4ng tr\u1ea3 v\u1ec1 g\u00ec.<\/span><\/td><td><span style=\"font-weight: 400;\">Lu\u00f4n tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">C\u00e1ch s\u1eed d\u1ee5ng<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng \u0111\u01b0\u1ee3c g\u1ecdi b\u1eb1ng l\u1ec7nh <\/span><span style=\"font-weight: 400;\">EXEC<\/span><span style=\"font-weight: 400;\">. Kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng tr\u1ef1c ti\u1ebfp trong c\u00e1c c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\">.<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 s\u1eed d\u1ee5ng trong c\u00e1c c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\">, ho\u1eb7c c\u00e1c ph\u00e9p to\u00e1n kh\u00e1c.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Thao t\u00e1c DML<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c DML (<\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\">).<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng \u0111\u01b0\u1ee3c ph\u00e9p s\u1eed d\u1ee5ng v\u00ec l\u00ed do an to\u00e0n d\u1eef li\u1ec7u, ch\u1ec9 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng SELECT.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Tham s\u1ed1<\/span><\/td><td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 tham s\u1ed1 IN, OUT, v\u00e0 INOUT.<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 h\u1ed7 tr\u1ee3 tham s\u1ed1 IN.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">X\u1eed l\u00fd l\u1ed7i<\/span><\/td><td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u00e1c kh\u1ed1i TRY-CATCH \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i.<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng kh\u00f4ng c\u00f3 c\u01a1 ch\u1ebf x\u1eed l\u00fd l\u1ed7i ph\u1ee9c t\u1ea1p nh\u01b0 th\u1ee7 t\u1ee5c.<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Hi\u1ec7u su\u1ea5t<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 t\u1ed1i \u01b0u h\u01a1n cho c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c bi\u00ean d\u1ecbch tr\u01b0\u1edbc, n\u00ean hi\u1ec7u su\u1ea5t cao h\u01a1n khi th\u1ef1c hi\u1ec7n nhi\u1ec1u l\u1ea7n.<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng ch\u1eadm h\u01a1n khi th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n ph\u1ee9c t\u1ea1p v\u00ec \u0111\u01b0\u1ee3c t\u00ednh to\u00e1n trong t\u1eebng truy v\u1ea5n.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/function-trong-sql\/\" target=\"_blank\" rel=\"noopener\"><strong>Function trong SQL l\u00e0 g\u00ec? H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng function trong SQL<\/strong><\/a><\/em><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-th\u1ee7-t\u1ee5c-co-h\u1ed7-tr\u1ee3-x\u1eed-ly-l\u1ed7i-khong\"><b>Th\u1ee7 t\u1ee5c c\u00f3 h\u1ed7 tr\u1ee3 x\u1eed l\u00fd l\u1ed7i kh\u00f4ng?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00f3, th\u1ee7 t\u1ee5c trong SQL h\u1ed7 tr\u1ee3 x\u1eed l\u00fd l\u1ed7i b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng kh\u1ed1i TRY-CATCH. Kh\u1ed1i n\u00e0y cho ph\u00e9p ph\u00e1t hi\u1ec7n v\u00e0 x\u1eed l\u00fd l\u1ed7i trong qu\u00e1 tr\u00ecnh th\u1ef1c thi, gi\u00fap duy tr\u00ec t\u00ednh \u1ed5n \u0111\u1ecbnh c\u1ee7a \u1ee9ng d\u1ee5ng v\u00e0 c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed b\u1ea1n c\u00f3 m\u1ed9t th\u1ee7 t\u1ee5c t\u00ednh to\u00e1n gi\u00e1 tr\u1ecb b\u1eb1ng c\u00e1ch chia 10 cho m\u1ed9t s\u1ed1. Trong qu\u00e1 tr\u00ecnh th\u1ef1c hi\u1ec7n, n\u1ebfu c\u00f3 l\u1ed7i x\u1ea3y ra (nh\u01b0 chia cho 0), kh\u1ed1i <\/span><span style=\"font-weight: 400;\">TRY-CATCH<\/span><span style=\"font-weight: 400;\"> s\u1ebd gi\u00fap x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng n\u00e0y. C\u1ee5 th\u1ec3, khi th\u1ee7 t\u1ee5c ch\u1ea1y, SQL s\u1ebd th\u1eed th\u1ef1c hi\u1ec7n ph\u00e9p chia trong kh\u1ed1i <\/span><span style=\"font-weight: 400;\">TRY<\/span><span style=\"font-weight: 400;\">. N\u1ebfu ph\u00e9p chia cho 0 x\u1ea3y ra, SQL s\u1ebd chuy\u1ec3n sang kh\u1ed1i <\/span><span style=\"font-weight: 400;\">CATCH<\/span><span style=\"font-weight: 400;\">, hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o l\u1ed7i &#8220;\u0110\u00e3 x\u1ea3y ra l\u1ed7i: chia cho 0&#8221;, gi\u00fap ng\u01b0\u1eddi d\u00f9ng d\u1ec5 d\u00e0ng nh\u1eadn bi\u1ebft v\u00e0 \u1ee9ng d\u1ee5ng kh\u00f4ng b\u1ecb gi\u00e1n \u0111o\u1ea1n.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-khi-nao-nen-s\u1eed-d\u1ee5ng-th\u1ee7-t\u1ee5c-trong-sql\"><b>Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL?<\/b><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">N\u00ean s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c khi c\u1ea7n th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 l\u1eb7p l\u1ea1i nh\u01b0 truy v\u1ea5n, c\u1eadp nh\u1eadt, x\u00f3a d\u1eef li\u1ec7u ho\u1eb7c x\u1eed l\u00fd logic ph\u1ee9c t\u1ea1p v\u00ec th\u1ee7 t\u1ee5c gi\u00fap t\u00e1i s\u1eed d\u1ee5ng m\u00e3, t\u0103ng hi\u1ec7u su\u1ea5t, b\u1ea3o m\u1eadt v\u00e0 gi\u1ea3m l\u01b0u l\u01b0\u1ee3ng m\u1ea1ng khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u l\u1edbn.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Xem th\u00eam: Danh s\u00e1ch <a href=\"https:\/\/itviec.com\/viec-lam-it\/sql?utm_medium=anchor_text_low&amp;utm_source=blog&amp;utm_campaign=viec_lam_sql&amp;utm_content=thu_tuc_trong_sql\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m SQL<\/a> m\u1edbi nh\u1ea5t tr\u00ean ITviec<\/strong><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-k\u1ebft-th\u1ee7-t\u1ee5c-trong-sql\"><span class=\"ez-toc-section\" id=\"Tong_ket_thu_tuc_trong_SQL\"><\/span><b>T\u1ed5ng k\u1ebft th\u1ee7 t\u1ee5c trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Th\u1ee7 t\u1ee5c trong SQL kh\u00f4ng ch\u1ec9 gi\u00fap t\u0103ng hi\u1ec7u su\u1ea5t v\u00e0 b\u1ea3o m\u1eadt c\u01a1 s\u1edf d\u1eef li\u1ec7u m\u00e0 c\u00f2n h\u1ed7 tr\u1ee3 t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c t\u00e1c v\u1ee5 l\u1eb7p l\u1ea1i. Vi\u1ec7c hi\u1ec3u v\u00e0 s\u1eed d\u1ee5ng th\u00e0nh th\u1ea1o th\u1ee7 t\u1ee5c s\u1ebd gi\u00fap b\u1ea1n qu\u1ea3n l\u00fd d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 h\u01a1n, gi\u1ea3m thi\u1ec3u sai s\u00f3t v\u00e0 t\u1ed1i \u01b0u h\u00f3a c\u00f4ng vi\u1ec7c.&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">ITviec hy v\u1ecdng qua b\u00e0i vi\u1ebft n\u00e0y, b\u1ea1n \u0111\u00e3 c\u00f3 c\u00e1i nh\u00ecn t\u1ed5ng quan v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>T\u00ecm hi\u1ec3u th\u00eam c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c l\u1ec7nh SQL nh\u01b0:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/itviec.com\/blog\/cau-lenh-delete-trong-sql\/\">C\u00e2u l\u1ec7nh DELETE trong SQL l\u00e0 g\u00ec? H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng chi ti\u1ebft<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/itviec.com\/blog\/cau-lenh-insert-trong-sql\/\">C\u00e2u l\u1ec7nh INSERT trong SQL: C\u00fa ph\u00e1p v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/itviec.com\/blog\/cau-lenh-update-trong-sql\/\">C\u00e2u l\u1ec7nh Update trong SQL: C\u00e1c th\u00e0nh ph\u1ea7n v\u00e0 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng<\/a><\/li>\n<\/ul>\n\n\n\n<p>V\u00e0 \u00f4n t\u1eadp th\u1eadt k\u1ef9 \u0111\u1ec3 ph\u1ecfng v\u1ea5n t\u1ef1 tin v\u1edbi <a href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-sql\/\" target=\"_blank\" rel=\"noreferrer noopener\">Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n SQL ph\u1ed5 bi\u1ebfn nh\u1ea5t<\/a> nh\u00e9!<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Th\u1ee7 t\u1ee5c (Stored Procedure) l\u00e0 m\u1ed9t ph\u1ea7n quan tr\u1ecdng trong SQL, gi\u00fap th\u1ef1c hi\u1ec7n t\u1ef1 \u0111\u1ed9ng c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p v\u00e0 l\u1eb7p l\u1ea1i. Vi\u1ec7c hi\u1ec3u r\u00f5 v\u00e0 n\u1eafm v\u1eefng c\u00e1ch s\u1eed d\u1ee5ng th\u1ee7 t\u1ee5c trong SQL s\u1ebd gi\u00fap t\u0103ng c\u01b0\u1eddng b\u1ea3o m\u1eadt, t\u0103ng hi\u1ec7u su\u1ea5t v\u00e0 gi\u1ea3m thi\u1ec3u r\u1ee7i ro cho c\u01a1 s\u1edf d\u1eef [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":81679,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-81401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3 - ITviec Blog<\/title>\n<meta name=\"description\" content=\"Kh\u00e1m ph\u00e1 th\u1ee7 t\u1ee5c trong SQL, bao g\u1ed3m \u0111\u1ecbnh ngh\u0129a, c\u00fa ph\u00e1p, c\u00e1ch s\u1eed d\u1ee5ng \u0111\u1ec3 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 v\u00e0 t\u0103ng c\u01b0\u1eddng b\u1ea3o m\u1eadt.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3\" \/>\n<meta property=\"og:description\" content=\"Th\u1ee7 t\u1ee5c (Stored Procedure) l\u00e0 m\u1ed9t ph\u1ea7n quan tr\u1ecdng trong SQL, gi\u00fap th\u1ef1c hi\u1ec7n t\u1ef1 \u0111\u1ed9ng c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p v\u00e0 l\u1eb7p l\u1ea1i. Vi\u1ec7c hi\u1ec3u r\u00f5 v\u00e0 n\u1eafm v\u1eefng c\u00e1ch s\u1eed d\u1ee5ng\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-25T02:08:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-10-11T16:55:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Thu-tuc-trong-SQL-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"M\u1ef9 Duy\u00ean\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"M\u1ef9 Duy\u00ean\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3 - ITviec Blog","description":"Kh\u00e1m ph\u00e1 th\u1ee7 t\u1ee5c trong SQL, bao g\u1ed3m \u0111\u1ecbnh ngh\u0129a, c\u00fa ph\u00e1p, c\u00e1ch s\u1eed d\u1ee5ng \u0111\u1ec3 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 v\u00e0 t\u0103ng c\u01b0\u1eddng b\u1ea3o m\u1eadt.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/","og_locale":"vi_VN","og_type":"article","og_title":"Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3","og_description":"Th\u1ee7 t\u1ee5c (Stored Procedure) l\u00e0 m\u1ed9t ph\u1ea7n quan tr\u1ecdng trong SQL, gi\u00fap th\u1ef1c hi\u1ec7n t\u1ef1 \u0111\u1ed9ng c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p v\u00e0 l\u1eb7p l\u1ea1i. Vi\u1ec7c hi\u1ec3u r\u00f5 v\u00e0 n\u1eafm v\u1eefng c\u00e1ch s\u1eed d\u1ee5ng","og_url":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-11-25T02:08:46+00:00","article_modified_time":"2025-10-11T16:55:44+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Thu-tuc-trong-SQL-vippro.jpg","type":"image\/jpeg"}],"author":"M\u1ef9 Duy\u00ean","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"M\u1ef9 Duy\u00ean","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"14 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3","datePublished":"2024-11-25T02:08:46+00:00","dateModified":"2025-10-11T16:55:44+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/"},"wordCount":3691,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Thu-tuc-trong-SQL-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/","url":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/","name":"Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3 - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Thu-tuc-trong-SQL-vippro.jpg","datePublished":"2024-11-25T02:08:46+00:00","dateModified":"2025-10-11T16:55:44+00:00","description":"Kh\u00e1m ph\u00e1 th\u1ee7 t\u1ee5c trong SQL, bao g\u1ed3m \u0111\u1ecbnh ngh\u0129a, c\u00fa ph\u00e1p, c\u00e1ch s\u1eed d\u1ee5ng \u0111\u1ec3 qu\u1ea3n l\u00fd c\u01a1 s\u1edf d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 v\u00e0 t\u0103ng c\u01b0\u1eddng b\u1ea3o m\u1eadt.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Thu-tuc-trong-SQL-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/Thu-tuc-trong-SQL-vippro.jpg","width":1500,"height":790,"caption":"Thu\u0309 tu\u0323c trong SQL - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/thu-tuc-trong-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"Th\u1ee7 t\u1ee5c trong SQL: H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng b\u1eb1ng v\u00ed d\u1ee5 c\u1ee5 th\u1ec3"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7","name":"M\u1ef9 Duy\u00ean","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","caption":"M\u1ef9 Duy\u00ean"},"url":"https:\/\/itviec.com\/blog\/author\/my-duyen\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/81401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/209"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=81401"}],"version-history":[{"count":1,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/81401\/revisions"}],"predecessor-version":[{"id":92166,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/81401\/revisions\/92166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/81679"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=81401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=81401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=81401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}