<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>PROCEDURE &#40;Thủ tục&#41; trong SQL Server</title>
<meta name="description" content="PROCEDURE &#40;Thủ tục&#41; trong SQL Server - Savefile - Tin Tức -...">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="PROCEDURE &#40;Thủ tục&#41; trong SQL Server">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;procedure-thu-tuc-trong-sql-server-7479.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/procedure-thu-tuc-trong-sql-server-7479.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/procedure-thu-tuc-trong-sql-server-7479.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>PROCEDURE &#40;Thủ tục&#41; trong SQL Server</h1>
		<ul class="list-inline">
			<li>Thứ hai - 28/08/2023 10:16</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="PROCEDURE &#40;Thủ tục&#41; trong SQL Server" src="https://st.quantrimang.com/photos/image/2018/02/24/ms-sql-server-select-200-size-80x80-znd.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p><strong>Procedure trong SQL Server&nbsp;</strong>có ý nghĩa thế nào trong lập trình?&nbsp;<strong>Cách sử dụng Procedure SQL Server</strong>&nbsp;có khó không? Hãy cùng Quantrimang.com tìm hiểu nhé!</p>

<p><strong>Procedure (Thủ tục)</strong>&nbsp;là một chương trình trong cơ sở dữ liệu gồm nhiều câu lệnh mà bạn lưu lại cho những lần sử dụng sau. Trong&nbsp;SQL Server, bạn có thể truyền các tham số vào procedure, tuy nó không trả về một giá trị cụ thể như function (hàm) nhưng cho biết việc thực thi thành công hay thất bại.</p>

<p><strong>Lợi ích khi dùng Procedure trong SQL</strong></p>

<ul>
	<li><strong>Dễ dàng chỉnh sửa</strong>: Lập trình viên dễ dàng chỉnh sửa code bên trong thủ tục ở SQL Server mà không cần khởi động lại hay triển khai ứng dụng. Ví dụ, nếu truy vấn T-SQL được viết trong ứng dụng và bạn cần thay đổi logic, bạn phải thay code trong ứng dụng đó và triển khai lại nó. Thủ tục trong SQL Server loại bỏ những khó khăn đó bằng cách lưu trữ code trong database. Vì thế, khi muốn thay đổi logic bên trong procedure, bạn chỉ có thể làm việc đó bằng lệnh Alter Procedure đơn giản.</li>
	<li><strong>Giảm tải truy cập mạng</strong>: Khi dùng procedure được lưu trữ thay vì viết các truy vấn T-SQL ở cấp ứng dụng, chỉ tên thủ tục được chuyển qua mạng thay vì toàn bộ code T-SQL.</li>
	<li><strong>Có thể tái sử dụng</strong>: Bạn có thể triển khai procedure được lưu trữ bằng nhiều người dùng hoặc nhiều ứng dụng client mà không cần viết lại code.</li>
	<li><strong>Bảo mật</strong>: Giảm các mối đe dọa bằng cách xóa truy cập trực tiếp tới bảng.</li>
</ul>

<p>Bài viết sẽ cung cấp cho bạn cú pháp và những ví dụ về cách tạo và xóa bỏ procedure trong SQL Server.</p>

<h2 id="mcetoc_1crp5mcn70">CREATE PROCEDURE (Tạo Procedure)</h2>

<h3 id="mcetoc_1crp5mflu1">Cú pháp</h3>

<p>Để tạo một procedure trong SQL Server, ta sử dụng cú pháp như dưới đây:</p>

<pre id="pre0">
CREATE { PROCEDURE | PROC } &#91;schema_name.&#93;procedure_name
&#91; @parameter &#91;type_schema_name.&#93; datatype
&#91; VARYING &#93; &#91; = default &#93; &#91; OUT | OUTPUT | READONLY &#93;
, @parameter &#91;type_schema_name.&#93; datatype
&#91; VARYING &#93; &#91; = default &#93; &#91; OUT | OUTPUT | READONLY &#93; &#93;

&#91; WITH { ENCRYPTION | RECOMPILE | EXECUTE AS Clause } &#93;
&#91; FOR REPLICATION &#93;

AS

BEGIN
&#91;declaration_section&#93;

executable_section

END;</pre>

<p><strong>Tham số:</strong></p>

<ul>
	<li><em>schema_name:</em>&nbsp;Tên schema (lược đồ) sở hữu procedure.</li>
	<li><em>procedure_name:</em>&nbsp;Tên gán cho procedure</li>
	<li><em>@parameter:</em>&nbsp;Một hay nhiều tham số được truyền vào hàm.</li>
	<li><em>type_schema_name:</em>&nbsp;Kiểu dữ liệu của schema (nếu có).</li>
	<li><em>Datatype:</em>&nbsp;Kiểu dữ liệu cho @parameter.</li>
	<li><em>Default:</em>&nbsp;Giá trị mặc định gán cho @parameter.</li>
	<li><em>OUT/</em><em>OUTPUT:</em>&nbsp;@parameter là một tham số đầu ra</li>
	<li><em>READONLY:</em>&nbsp;@parameter không thể bị procedure ghi đè lên.</li>
	<li><em>ENCRYPTION:</em>&nbsp;Mã nguồn (source) của procedure sẽ không được lưu trữ dưới dạng text trong hệ thống.</li>
	<li><em>RECOMPILE:</em>&nbsp;Truy vấn sẽ không được lưu ở bộ nhớ đệm (cache) cho thủ tục này.</li>
	<li><em>EXECUTE AS clause:&nbsp;</em>Xác định ngữ cảnh bảo mật để thực thi thủ tục.</li>
	<li><em>FOR REPLICATION:</em>&nbsp;Procedure đã lưu sẽ chỉ được thực thi trong quá trình replication (nhân bản).</li>
</ul>

<h3 id="mcetoc_1crp5mmoa2">Ví dụ</h3>

<pre id="pre1">
CREATE PROCEDURE spNhanvien
@nhanvien_name VARCHAR(50) OUT

AS
BEGIN
DECLARE @nhanvien_id INT;
SET @nhanvien_id = 8;
IF @nhanvien_id &lt; 10
SET @nhanvien_name = &#039;Smith&#039;;
ELSE
SET @nhanvien_name = &#039;Lawrence&#039;;

END;</pre>

<p>Thủ tục trên được gán tên là&nbsp;<em>spNhanvien,</em>&nbsp;có một tham số là&nbsp;<em>@nhanvien_name,&nbsp;</em>output của tham số sẽ được dựa trên&nbsp;<em>@nhanvien_id.</em></p>

<p>Sau đó, bạn có thể thực hiện tham chiếu&nbsp;<em>spNhanvien</em>&nbsp;như sau:</p>

<pre id="pre2">
USE &#91;test&#93;
GO

DECLARE @site_name varchar(50);
EXEC FindSite @site_name OUT;
PRINT @site_name;

GO</pre>

<h2 id="mcetoc_1croiu6ua3">Drop Procedure (Xóa bỏ Procedure)</h2>

<p>Một khi đã tạo thành công các procedure thì cũng sẽ có những trường hợp bạn muốn xóa bỏ procedure khỏi cơ sở dữ liệu vì một vài lý do.</p>

<h3 id="mcetoc_1crmkpbng4">Cú pháp</h3>

<p>Để xóa bỏ một procedure, ta có cú pháp sau:</p>

<pre id="pre3">
DROP PROCEDURE procedure_name;</pre>

<p><strong>Tham số:</strong></p>

<p><em>procedure_name:</em>&nbsp;Tên procedure bạn muốn xóa bỏ...</p>

<h3 id="mcetoc_1crmkpbng5">Ví dụ</h3>

<pre id="pre4">
DROP PROCEDURE spNhanvien;</pre>

<p>Thực hiện lệnh này là bạn đã vừa xóa bỏ thủ tục&nbsp;<em>spNhanvien</em>&nbsp;khỏi database.</p>

<h2>Thủ tục lưu trữ tạm thời</h2>

<p>Bạn có thể tạo các thủ tục tạm thời tương tự như tạo bảng tạm thời. Database tempdb được dùng để tạo những thủ tục này. Bạn có thể chia thủ tục tạm thời thành 2 loại:</p>

<ul>
	<li>Thủ tục lưu trữ tạm thời cục bộ</li>
	<li>Thủ tục tạm thời cục bộ</li>
</ul>

<p>Thủ tục lưu trữ tạm thời cục bộ: Bạn có thể tạo kiểu này bằng cách dùng tiền tố # và chỉ truy cập trong phiên mà chúng được tạo. Khi đóng kết nối, quá trình này ngay lập tức kết thúc.</p>

<p>Ví dụ cách tạo một thủ tục tạm thời nội bộ:</p>

<pre id="pre5">
CREATE PROCEDURE #Temp
AS
BEGIN
PRINT &#039;Local temp procedure&#039;
END</pre>

<p>Thủ tục lưu trữ tạm thời toàn cục: Bạn có thể tạo kiểu này bằng cách dùng ## làm tiền tố và truy cập từ phiên bất kỳ. Khi kết nối được dùng để tạo quá trình này đóng lại, thủ tục sẽ tự động kết thúc.</p>

<p>Ví dụ cách tạo thủ tục tạm thời toàn cục:</p>

<pre id="pre6">
CREATE PROCEDURE ##TEMP
AS
BEGIN
PRINT &#039;Global temp procedure&#039;
END</pre>

<h2>Những hạn chế của Procedure SQL Server</h2>

<ul>
	<li><strong>Gỡ lỗi</strong>: Vì gỡ lỗi các thủ tục được lưu trữ chưa bao giờ đơn giản nên bạn không nên viết và triển khai các logic nghiệp vụ phức tạp bằng cách sử dụng chúng. Nếu không, kết quả dễ bị lỗi vì không được xử lý chính xác.</li>
	<li><strong>Phụ thuộc:</strong>&nbsp;Như đã biết, lập trình viên DBA và cơ sở dữ liệu chuyên nghiệp xử lý tập dữ liệu “khủng” ở các tổ chức lớn. Lập trình viên ứng dụng phải phụ thuộc vào họ bởi bất kỳ thay đổi nhỏ đều phải tham chiếu tới DBA - người có thể sửa lỗi trong các thủ tục hiện tại hoặc xây dựng mới.</li>
	<li><strong>Đắt</strong>: Tạo thủ tục trong SQL Server thường có chi phí quản lý DBA đắt đỏ vì các tổ chức phải trả thêm tiền cho DBA chuyên nghiệp. DBA có chuyên môn, kỹ thuật cao xử lý các thủ tục database phức tạp tốt hơn.</li>
	<li><strong>Chuyên dành cho nhà cung cấp</strong>: Các thủ tục lưu trữ được viết trong một nền tảng không thể chạy trên các nền tảng khác. Vì thủ tục được viết ở Oracle phức tạp hơn nên bạn cần viết lại toàn bộ procedure cho SQL Server.</li>
</ul>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/procedure-thu-tuc-trong-sql-server-7479.html" title="PROCEDURE &#40;Thủ tục&#41; trong SQL Server">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/procedure-thu-tuc-trong-sql-server-7479.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>